---
title: "Nitro Hooks"
description: "Learn how to use Nitro hooks to modify the robots final output."
canonical_url: "https://nuxtseo.com/docs/robots/nitro-api/nitro-hooks"
last_updated: "2026-05-09T02:21:32.173Z"
---

## `'robots:config'`

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

```ts
interface HookContext {
  groups: RobotsGroupResolved[]
  sitemaps: string[]
  context: 'robots.txt' | 'init'
  event?: H3Event // 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 the robots.txt `robots.txt`.

```ts [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 Indexing

Use `ctx.event` to apply different rules based on the request hostname.

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

## `'robots:robots-txt'`

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

```ts
export interface HookRobotsTxtContext {
  robotsTxt: string
  e: H3Event
}
```

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

```ts [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()
    })
  }
})
```
