Index of every page on this site: llms.txt

Nitro Hooks

'robots:config'

Type: (ctx: HookContext) => void | Promise<void>

interface HookContext {
  groups: RobotsGroupResolved[]
  sitemaps: string[]
  context: 'robots.txt' | 'init'
  event?: RequestEvent // undefined on `init`
  errors: string[]
  warnings: string[]
}

Modify the robots config before it's used to generate the indexing rules.

This is called when Nitro starts (init) as well as when generating robots.txt.

server/plugins/robots-ignore-routes.ts
export default defineNitroPlugin((nitroApp) => {
  nitroApp.hooks.hook('robots:config', async (ctx) => {
    // extend the robot.txt rules at runtime
    if (ctx.context === 'init') {
      // probably want to cache this
      const ignoredRoutes = await $fetch('/api/ignored-routes')
      ctx.groups[0].disallow.push(...ignoredRoutes)
    }
  })
})

Per-domain robots.txt. Use ctx.event to change this request's robots.txt output. These changes do not change indexing rules on later page requests. For host-based page indexing, use Site Config hooks.

server/plugins/robots-domain.ts
export default defineNitroPlugin((nitroApp) => {
  nitroApp.hooks.hook('robots:config', (ctx) => {
    const host = ctx.event?.req.headers.get('host')
    if (host?.includes('staging') || host?.includes('test')) {
      ctx.groups[0].disallow = ['/']
    }
  })
})

'robots:robots-txt'

Type: (ctx: HookContext) => void | Promise<void>

export interface HookRobotsTxtContext {
  robotsTxt: string
  e: RequestEvent
}

This hook allows you to modify the robots.txt content before it is sent to the client.

server/plugins/robots-remove-comments.ts
export default defineNitroPlugin((nitroApp) => {
  if (!import.meta.dev) {
    nitroApp.hooks.hook('robots:robots-txt', async (ctx) => {
      // remove comments from robotsTxt in production
      ctx.robotsTxt = ctx.robotsTxt.replace(/^#.*$/gm, '').trim()
    })
  }
})
Was this page helpful?