125 lines
3.7 KiB
JavaScript
125 lines
3.7 KiB
JavaScript
/**
|
|
* Known AI user agents, split by purpose:
|
|
* - assistant/search bots fetch a page to answer a user's question live or to
|
|
* power an AI search index — the ones that make "the chat reads your site"
|
|
* work, and that most businesses WANT.
|
|
* - training crawlers collect pages to train models — some clients prefer to
|
|
* opt out of these while keeping assistant/search visibility.
|
|
*/ export const AI_ASSISTANT_BOTS = [
|
|
'ClaudeBot',
|
|
'Claude-Web',
|
|
'Claude-SearchBot',
|
|
'ChatGPT-User',
|
|
'OAI-SearchBot',
|
|
'PerplexityBot',
|
|
'Perplexity-User'
|
|
];
|
|
export const AI_TRAINING_BOTS = [
|
|
'GPTBot',
|
|
'Google-Extended',
|
|
'CCBot',
|
|
'anthropic-ai',
|
|
'Applebot-Extended',
|
|
'Bytespider',
|
|
'Meta-ExternalAgent'
|
|
];
|
|
/**
|
|
* Builds robots rules that allow crawling everything except the admin panel and
|
|
* API, point crawlers at the sitemap, and (by default) explicitly welcome AI
|
|
* assistant/search/training bots.
|
|
*
|
|
* ```ts
|
|
* // app/robots.ts
|
|
* export default function robots() {
|
|
* return buildRobots({ baseUrl: process.env.NEXT_PUBLIC_SERVER_URL! })
|
|
* }
|
|
* ```
|
|
*
|
|
* The admin and API are disallowed by default — no reason for them in the index,
|
|
* and it keeps crawl budget on real content.
|
|
*
|
|
* AI visibility: the #1 reason "the chat won't read the site" is an AI user
|
|
* agent being blocked (or not explicitly allowed under a tightened '*' rule).
|
|
* `aiBots: 'allow'` (default) emits explicit allow rules for ClaudeBot, GPTBot,
|
|
* PerplexityBot, Google-Extended and friends. To keep AI answers but opt out of
|
|
* model training, use `aiBots: 'block-training'`. To block them all, 'disallow'.
|
|
*
|
|
* Block parametrized/thin URLs that waste crawl budget — added to the defaults:
|
|
* ```ts
|
|
* buildRobots({ baseUrl, disallowParams: ['s', 'meter'] })
|
|
* // Disallow: /admin, /api, /*?s=, /*&s=, /*?meter=, /*&meter=
|
|
* ```
|
|
*/ export function buildRobots({ adminRoute = '/admin', aiAssistantAgents = AI_ASSISTANT_BOTS, aiBots = 'allow', aiTrainingAgents = AI_TRAINING_BOTS, baseUrl, disallow: disallowOverride, disallowParams = [], extraDisallow = [], sitemap = true }) {
|
|
const origin = baseUrl.replace(/\/$/, '');
|
|
const defaults = [
|
|
...adminRoute ? [
|
|
adminRoute
|
|
] : [],
|
|
'/api'
|
|
];
|
|
const paramRules = disallowParams.map((p)=>p.trim()).filter(Boolean).flatMap((p)=>[
|
|
`/*?${p}=`,
|
|
`/*&${p}=`
|
|
]);
|
|
const disallow = [
|
|
...new Set([
|
|
...disallowOverride ?? defaults,
|
|
...extraDisallow,
|
|
...paramRules
|
|
])
|
|
];
|
|
const baseRule = {
|
|
allow: '/',
|
|
disallow,
|
|
userAgent: '*'
|
|
};
|
|
const aiRules = [];
|
|
if (aiBots === 'allow') {
|
|
aiRules.push({
|
|
allow: '/',
|
|
disallow,
|
|
userAgent: [
|
|
...aiAssistantAgents,
|
|
...aiTrainingAgents
|
|
]
|
|
});
|
|
} else if (aiBots === 'block-training') {
|
|
aiRules.push({
|
|
allow: '/',
|
|
disallow,
|
|
userAgent: [
|
|
...aiAssistantAgents
|
|
]
|
|
});
|
|
aiRules.push({
|
|
disallow: [
|
|
'/'
|
|
],
|
|
userAgent: [
|
|
...aiTrainingAgents
|
|
]
|
|
});
|
|
} else if (aiBots === 'disallow') {
|
|
aiRules.push({
|
|
disallow: [
|
|
'/'
|
|
],
|
|
userAgent: [
|
|
...aiAssistantAgents,
|
|
...aiTrainingAgents
|
|
]
|
|
});
|
|
}
|
|
return {
|
|
rules: aiRules.length > 0 ? [
|
|
baseRule,
|
|
...aiRules
|
|
] : baseRule,
|
|
...sitemap ? {
|
|
sitemap: `${origin}/sitemap.xml`
|
|
} : {},
|
|
host: origin
|
|
};
|
|
}
|
|
|
|
//# sourceMappingURL=buildRobots.js.map
|