Replaced by an explicit list of forms only (ten exported names, runtime env names, project file names and marks, commands, skill names, the log prefix, the brand in prose). Package name, import paths, routes, CSS tokens, data attributes, custom keys, context flags, the cookie and kv keys are not touched. package.json keeps the five old bin names beside the five new ones. The old names stop working in this commit and come back as aliases in the next; docs follow. Co-Authored-By: Claude Fable 5.2 <[email protected]>
188 lines
6.9 KiB
TypeScript
188 lines
6.9 KiB
TypeScript
import { isSiteNoindex } from './siteNoindex.js'
|
|
|
|
type RobotRule = { allow?: string; disallow?: string[]; userAgent: string | string[] }
|
|
|
|
/**
|
|
* 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-User',
|
|
'Claude-Web',
|
|
'Claude-SearchBot',
|
|
'ChatGPT-User',
|
|
'OAI-SearchBot',
|
|
'PerplexityBot',
|
|
'Perplexity-User',
|
|
'MistralAI-User',
|
|
'DuckAssistBot',
|
|
] as const
|
|
|
|
export const AI_TRAINING_BOTS = [
|
|
'GPTBot',
|
|
'Google-Extended',
|
|
'CCBot',
|
|
'anthropic-ai',
|
|
'Applebot-Extended',
|
|
'Bytespider',
|
|
'Meta-ExternalAgent',
|
|
] as const
|
|
|
|
export type AiBotsPolicy = 'allow' | 'block-training' | 'disallow' | false
|
|
|
|
/** One bot treated differently from the policy, e.g. { userAgent: 'Bytespider', access: 'disallow' }. */
|
|
export type AiBotRule = { access: 'allow' | 'disallow'; userAgent: string }
|
|
|
|
export type RobotsOptions = {
|
|
/**
|
|
* Admin path to disallow. Default '/admin'. With a hidden panel
|
|
* (iapl adminRoute) pass `false`: robots.txt is public, so listing the
|
|
* path would reveal it — the admin is noindex anyway.
|
|
*/
|
|
adminRoute?: false | string
|
|
/** Override the assistant/search bot list. */
|
|
aiAssistantAgents?: readonly string[]
|
|
/**
|
|
* Per-bot exceptions that win over `aiBots` — a listed bot leaves its policy
|
|
* group and gets its own allow or block. Unknown bots can be added this way.
|
|
* Site Settings → Search engines fills it when you use `getRobots`.
|
|
*/
|
|
aiBotRules?: readonly AiBotRule[]
|
|
/**
|
|
* How to treat AI crawlers (assistant/search + training). Default 'allow' —
|
|
* explicitly welcome them so the site stays visible to Claude, ChatGPT,
|
|
* Perplexity etc. and an accidental future '*' tightening doesn't lock them
|
|
* out. Options:
|
|
* - 'allow' (default): explicit allow for assistant AND training bots.
|
|
* - 'block-training': allow assistant/search bots, disallow training crawlers
|
|
* — keep AI answers/visibility, opt out of model training.
|
|
* - 'disallow': block all known AI bots.
|
|
* - false: emit no AI-specific rules (only the '*' rule).
|
|
*/
|
|
aiBots?: AiBotsPolicy
|
|
/** Override the training crawler list. */
|
|
aiTrainingAgents?: readonly string[]
|
|
/**
|
|
* Full disallow list — REPLACES the defaults ([adminRoute, '/api']). Prefer
|
|
* `extraDisallow` / `disallowParams`, which add to the defaults instead.
|
|
*/
|
|
disallow?: string[]
|
|
/**
|
|
* Query parameters whose URLs crawlers should skip (thin/duplicate pages that
|
|
* waste crawl budget), e.g. ['s', 'meter', 'utm_source']. Each becomes
|
|
* `/*?name=` and `/*&name=` (the parameter may not be first).
|
|
* Don't list `page` — paginated archives are real, canonical-to-self pages.
|
|
*/
|
|
disallowParams?: string[]
|
|
/** Paths added to the default disallow list, e.g. ['/koszyk']. */
|
|
extraDisallow?: string[]
|
|
/** Whether to advertise the sitemap. Defaults to true. */
|
|
sitemap?: boolean
|
|
}
|
|
|
|
type BuildRobotsArgs = {
|
|
/** Absolute origin, e.g. 'https://example.com'. */
|
|
baseUrl: string
|
|
} & RobotsOptions
|
|
|
|
/**
|
|
* Shape for Next's `app/robots.ts`. `rules` may be a single rule or an array
|
|
* (Next accepts both) — it becomes an array when AI-bot rules are added.
|
|
*/
|
|
export type RobotsRules = {
|
|
host?: string
|
|
rules: RobotRule | RobotRule[]
|
|
sitemap?: string
|
|
}
|
|
|
|
/**
|
|
* 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,
|
|
aiBotRules = [],
|
|
aiBots = 'allow',
|
|
aiTrainingAgents = AI_TRAINING_BOTS,
|
|
baseUrl,
|
|
disallow: disallowOverride,
|
|
disallowParams = [],
|
|
extraDisallow = [],
|
|
sitemap = true,
|
|
}: BuildRobotsArgs): RobotsRules {
|
|
const origin = baseUrl.replace(/\/$/, '')
|
|
// Staging / preview (IAPL_NOINDEX=1): nothing may be crawled.
|
|
if (isSiteNoindex()) {return { rules: { disallow: ['/'], userAgent: '*' } }}
|
|
|
|
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: RobotRule = { allow: '/', disallow, userAgent: '*' }
|
|
|
|
// Exceptions: last rule per bot wins; those bots leave the policy groups.
|
|
const exceptions = new Map<string, AiBotRule>()
|
|
for (const r of aiBotRules) {
|
|
const ua = r.userAgent.trim()
|
|
if (ua) {exceptions.set(ua.toLowerCase(), { access: r.access, userAgent: ua })}
|
|
}
|
|
const notExcepted = (list: readonly string[]) => list.filter((ua) => !exceptions.has(ua.toLowerCase()))
|
|
const assistants = notExcepted(aiAssistantAgents)
|
|
const training = notExcepted(aiTrainingAgents)
|
|
|
|
const aiRules: RobotRule[] = []
|
|
const push = (rule: RobotRule) => {
|
|
if (rule.userAgent.length) {aiRules.push(rule)}
|
|
}
|
|
if (aiBots === 'allow') {
|
|
push({ allow: '/', disallow, userAgent: [...assistants, ...training] })
|
|
} else if (aiBots === 'block-training') {
|
|
push({ allow: '/', disallow, userAgent: assistants })
|
|
push({ disallow: ['/'], userAgent: training })
|
|
} else if (aiBots === 'disallow') {
|
|
push({ disallow: ['/'], userAgent: [...assistants, ...training] })
|
|
}
|
|
const allowed = [...exceptions.values()].filter((r) => r.access === 'allow').map((r) => r.userAgent)
|
|
const blocked = [...exceptions.values()].filter((r) => r.access === 'disallow').map((r) => r.userAgent)
|
|
push({ allow: '/', disallow, userAgent: allowed })
|
|
push({ disallow: ['/'], userAgent: blocked })
|
|
|
|
return {
|
|
rules: aiRules.length > 0 ? [baseRule, ...aiRules] : baseRule,
|
|
...(sitemap ? { sitemap: `${origin}/sitemap.xml` } : {}),
|
|
host: origin,
|
|
}
|
|
}
|