Nitro Hooks · Nuxt Robots · Nuxt SEO

-
-
-
-

[1.4K](https://github.com/harlan-zw/nuxt-seo)

[Nuxt SEO on GitHub](https://github.com/harlan-zw/nuxt-seo)

Robots

-
-
-
-
-
-
-
-
-
-

Search…```k`` /`

v6.0.7

- Playgrounds
- [Discord Support](https://discord.com/invite/275MBUBvgP)

### Nuxt API

-
-
-
-

### Nitro API

-
-
-
-

Nitro API

# Nitro Hooks

[Copy for LLMs](https://nuxtseo.com/docs/robots/nitro-api/nitro-hooks.md)

## [`'robots:config'`](#robotsconfig)

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

```
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`.

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](#per-domain-indexing)

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

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'`](#robotsrobots-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()
    })
  }
})
```

[Edit this page](https://github.com/nuxt-modules/robots/edit/main/docs/content/3.nitro-api/2.nitro-hooks.md)

[Markdown For LLMs](https://nuxtseo.com/docs/robots/nitro-api/nitro-hooks.md)

Did this page help you?

On this page

- ['robots:config'](#robotsconfig)
- ['robots:robots-txt'](#robotsrobots-txt)

[GitHub](https://github.com/harlan-zw/nuxt-seo) [ Discord](https://discord.com/invite/275MBUBvgP)

###

-
-

Modules

-
-
-
-
-
-
-
-
-

###

-
-
-

###

Nuxt

-
-
-
-
-

Vue

-
-
-
-
-
-
-
-

###

-
-
-
-
-
-
-
-
-
-

Copyright © 2023-2026 Harlan Wilton - [MIT License](https://github.com/harlan-zw/nuxt-seo/blob/main/license) · [mdream](https://mdream.dev)