Nitro Hooks

Learn how to use Nitro hooks to modify the robots final output.

'robots:config'

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

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

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 the robots.txt 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)
    }
  })
})

'robots:robots-txt'

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

export interface HookRobotsTxtContext {
  robotsTxt: string
  e: H3Event
}

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 (!process.dev) {
    nitroApp.hooks.hook('robots:robots-txt', async (ctx) => {
      // remove comments from robotsTxt in production
      ctx.robotsTxt = ctx.robotsTxt.replace(/^#.*$/gm, '').trim()
    })
  }
})
Did this page help you?