SEO/GEO/GSC: sitemap index + caching, Search & AI settings, getRobots
- Sitemap: batched reads (500), published-only where, field select; sitemap index over maxUrls (default 10k) with ?part=N; Cache-Control; in-process memo; runtime errors → 503 Retry-After (empty urlset only during next build) - Site Settings → Search & AI tab (seo.searchSettings: false removes it): Google/Bing/Yandex verification (paste code or whole meta tag) added to page metadata; AI crawler policy + per-bot exceptions; llms.txt note - buildRobots aiBotRules; getRobots helper reads the panel; more AI agents - Under construction: sitemap and llms.txt list only open pages - collectSiteIndex: no silent 1000 cap; llms routes send Cache-Control - IndexNow: nothing sent with IPAL_NOINDEX - Unknown route metadata is noindex - Tests (unit + SQLite integration) and docs/seo.md Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
12fbd424a5
commit
94e563055f
@@ -0,0 +1,170 @@
|
||||
import type { Field } from 'payload'
|
||||
|
||||
import type { AiBotRule, AiBotsPolicy, RobotsOptions } from './buildRobots.js'
|
||||
|
||||
/**
|
||||
* Site Settings → Search & AI: what editors set for search engines and AI
|
||||
* assistants without a deploy — Search Console / Bing verification codes, the
|
||||
* AI crawler policy for robots.txt, and a note for llms.txt. Empty fields
|
||||
* change nothing: verification tags appear only once a code is pasted, and the
|
||||
* robots policy stays the one set in code until an editor picks another.
|
||||
*/
|
||||
|
||||
export const SEARCH_VERIFICATION_FIELD = 'searchVerification'
|
||||
export const AI_CRAWLERS_FIELD = 'aiCrawlers'
|
||||
export const LLMS_INTRO_FIELD = 'llmsIntro'
|
||||
|
||||
/** Next `Metadata.verification` — site ownership tags for search consoles. */
|
||||
export type VerificationMetadata = {
|
||||
google?: string
|
||||
other?: Record<string, string>
|
||||
yandex?: string
|
||||
}
|
||||
|
||||
/**
|
||||
* Accepts the bare code or the whole tag an editor copies from Search Console
|
||||
* (`<meta name="google-site-verification" content="abc…" />`) and keeps only
|
||||
* the code. Anything that isn't a plain token is dropped — the value ends up
|
||||
* in every page's <head>.
|
||||
*/
|
||||
export function normalizeVerificationToken(raw: unknown): string {
|
||||
if (typeof raw !== 'string') {return ''}
|
||||
let value = raw.trim()
|
||||
const content = /content\s*=\s*["']([^"']*)["']/i.exec(value)
|
||||
if (content) {value = content[1].trim()}
|
||||
value = value.replace(/^["']|["']$/g, '')
|
||||
return /^[\w\-.:+/=]{1,200}$/.test(value) ? value : ''
|
||||
}
|
||||
|
||||
const tokenField = (name: string, label: string, description: string): Field => ({
|
||||
name,
|
||||
type: 'text',
|
||||
admin: { description },
|
||||
hooks: { beforeChange: [({ value }) => normalizeVerificationToken(value) || null] },
|
||||
label,
|
||||
})
|
||||
|
||||
const AI_POLICY_OPTIONS = [
|
||||
{ label: 'As set by the developer (default)', value: 'default' },
|
||||
{ label: 'Allow all AI assistants and AI training', value: 'allow' },
|
||||
{ label: 'Allow AI assistants and AI search, block AI training', value: 'block-training' },
|
||||
{ label: 'Block all known AI bots', value: 'disallow' },
|
||||
] as const
|
||||
|
||||
/** The "Search & AI" tab fields. Not localized, except the llms.txt note. */
|
||||
export const searchSettingsFields: Field[] = [
|
||||
{
|
||||
name: SEARCH_VERIFICATION_FIELD,
|
||||
type: 'group',
|
||||
admin: {
|
||||
description:
|
||||
'Proves to search engines that you own this site. Paste the code (or the whole <meta> tag) from the search engine; it is added to every page. Leave empty if you verify another way (DNS, file).',
|
||||
},
|
||||
fields: [
|
||||
tokenField('google', 'Google Search Console', 'Search Console → Settings → Ownership verification → HTML tag.'),
|
||||
tokenField('bing', 'Bing Webmaster Tools', 'Bing Webmaster Tools → Add site → HTML Meta Tag (msvalidate.01). Bing also powers ChatGPT Search and Copilot.'),
|
||||
tokenField('yandex', 'Yandex Webmaster', 'Yandex Webmaster → Site verification → Meta tag.'),
|
||||
],
|
||||
label: 'Search engine verification',
|
||||
},
|
||||
{
|
||||
name: AI_CRAWLERS_FIELD,
|
||||
type: 'group',
|
||||
admin: {
|
||||
description:
|
||||
'Which AI bots may read the site (robots.txt). Assistants (ClaudeBot, ChatGPT-User, PerplexityBot…) fetch pages to answer questions and cite you; training crawlers (GPTBot, Google-Extended, CCBot…) collect pages to train models. Blocking assistants removes the site from AI answers.',
|
||||
},
|
||||
fields: [
|
||||
{
|
||||
name: 'policy',
|
||||
type: 'select',
|
||||
defaultValue: 'default',
|
||||
label: 'AI bots',
|
||||
options: AI_POLICY_OPTIONS.map((o) => ({ ...o })),
|
||||
},
|
||||
{
|
||||
name: 'rules',
|
||||
type: 'array',
|
||||
admin: {
|
||||
description:
|
||||
'Exceptions for single bots, by user agent exactly as the bot announces itself (e.g. GPTBot, Bytespider). Wins over the setting above.',
|
||||
initCollapsed: true,
|
||||
},
|
||||
fields: [
|
||||
{ name: 'userAgent', type: 'text', label: 'User agent', required: true },
|
||||
{
|
||||
name: 'access',
|
||||
type: 'select',
|
||||
defaultValue: 'disallow',
|
||||
options: [
|
||||
{ label: 'Allow', value: 'allow' },
|
||||
{ label: 'Block', value: 'disallow' },
|
||||
],
|
||||
required: true,
|
||||
},
|
||||
],
|
||||
label: 'Per-bot exceptions',
|
||||
},
|
||||
],
|
||||
label: 'AI crawlers',
|
||||
},
|
||||
{
|
||||
name: LLMS_INTRO_FIELD,
|
||||
type: 'textarea',
|
||||
admin: {
|
||||
description:
|
||||
'Shown at the top of /llms.txt, under the site description: key facts an AI assistant should know and may quote — what you do, where, for whom, since when. Plain sentences, no marketing slogans.',
|
||||
},
|
||||
label: 'About the site for AI assistants (llms.txt)',
|
||||
localized: true,
|
||||
},
|
||||
]
|
||||
|
||||
type SettingsLike = null | Record<string, unknown> | undefined
|
||||
|
||||
/** Site Settings → verification codes as Next `Metadata.verification`; undefined when none is set. */
|
||||
export function verificationFrom(settings: SettingsLike): undefined | VerificationMetadata {
|
||||
const group = settings?.[SEARCH_VERIFICATION_FIELD] as null | Record<string, unknown> | undefined
|
||||
if (!group || typeof group !== 'object') {return undefined}
|
||||
const google = normalizeVerificationToken(group.google)
|
||||
const bing = normalizeVerificationToken(group.bing)
|
||||
const yandex = normalizeVerificationToken(group.yandex)
|
||||
if (!google && !bing && !yandex) {return undefined}
|
||||
return {
|
||||
...(google ? { google } : {}),
|
||||
...(yandex ? { yandex } : {}),
|
||||
...(bing ? { other: { 'msvalidate.01': bing } } : {}),
|
||||
}
|
||||
}
|
||||
|
||||
const POLICIES = new Set<string>(['allow', 'block-training', 'disallow'])
|
||||
|
||||
/**
|
||||
* Site Settings → AI crawlers as buildRobots options. Only what the editor
|
||||
* changed: policy 'default' keeps the option from code, no rules adds none.
|
||||
*/
|
||||
export function robotsOptionsFrom(settings: SettingsLike): Pick<RobotsOptions, 'aiBotRules' | 'aiBots'> {
|
||||
const group = settings?.[AI_CRAWLERS_FIELD] as { policy?: unknown; rules?: unknown } | null | undefined
|
||||
if (!group || typeof group !== 'object') {return {}}
|
||||
const policy = typeof group.policy === 'string' && POLICIES.has(group.policy) ? (group.policy as AiBotsPolicy) : undefined
|
||||
const rules: AiBotRule[] = Array.isArray(group.rules)
|
||||
? group.rules.flatMap((r: unknown) => {
|
||||
const row = r as { access?: unknown; userAgent?: unknown } | null
|
||||
// A user agent is one token — no spaces, colons or newlines that could
|
||||
// inject other robots.txt lines.
|
||||
const ua = typeof row?.userAgent === 'string' ? row.userAgent.trim() : ''
|
||||
if (!/^[\w\-.*/]{1,100}$/.test(ua)) {return []}
|
||||
return [{ access: row?.access === 'allow' ? ('allow' as const) : ('disallow' as const), userAgent: ua }]
|
||||
})
|
||||
: []
|
||||
return {
|
||||
...(policy ? { aiBots: policy } : {}),
|
||||
...(rules.length ? { aiBotRules: rules } : {}),
|
||||
}
|
||||
}
|
||||
|
||||
/** Site Settings → llms.txt note in the locale it was read in. Empty string when unset. */
|
||||
export function llmsIntroFrom(settings: SettingsLike): string {
|
||||
const v = settings?.[LLMS_INTRO_FIELD]
|
||||
return typeof v === 'string' ? v.trim() : ''
|
||||
}
|
||||
Reference in New Issue
Block a user