Robots
Nitro Api
Nitro Hooks
Learn how to use Nitro hooks to modify the robots final output.
robots:config
Type:
interface HookContext {
groups: RobotsGroupResolved[]
sitemaps: string[]
context: 'robots.txt' | 'init'
event?: H3Event // undefined on `init`
}
Modify the sitemap 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) => {
// probably want to cache this
const ignoredRoutes = await $fetch('/api/ignored-routes')
ctx.groups[0].disallow.push(...ignoredRoutes)
})
})
robots:robots-txt
Type:
interface HookContext {
e: H3Event
robotsTxt: string
}
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()
})
}
})