- 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]>
125 lines
4.7 KiB
TypeScript
125 lines
4.7 KiB
TypeScript
import type { SitemapEntry } from './buildSitemapEntries.js'
|
|
|
|
type BuildSitemapXmlOptions = {
|
|
/**
|
|
* URL of a CSS stylesheet to make the sitemap readable in the browser, e.g.
|
|
* '/sitemap.css'. Uses `type="text/css"` — the W3C "Associating Style Sheets
|
|
* with XML" mechanism, which is NOT deprecated (unlike XSLT / type="text/xsl",
|
|
* which Chrome/WebKit are removing). CSS on XML shows no warning, styles the
|
|
* raw tags directly (e.g. `url { display: block }` turns the wall of text into
|
|
* cards), and crawlers ignore the directive entirely.
|
|
*/
|
|
cssUrl?: string
|
|
}
|
|
|
|
/**
|
|
* Serializes sitemap entries to a clean, indented XML STRING — valid for crawlers
|
|
* and (with `cssUrl`) styled in the browser via plain CSS.
|
|
*
|
|
* Two viewing modes:
|
|
* - No cssUrl → the browser's native formatted XML tree (indented, collapsible).
|
|
* - With cssUrl → a `<?xml-stylesheet type="text/css">` directive; the project's
|
|
* CSS styles the XML tags (cards, labels via ::before). NOT XSLT — that's being
|
|
* removed from browsers and shows a deprecation warning. CSS is safe and
|
|
* W3C-standard.
|
|
*
|
|
* // app/sitemap.xml/route.ts
|
|
* import { buildSitemapXml } from '@intecion/ipal-kit'
|
|
* import { sitemap } from '@/lib/content'
|
|
* export const dynamic = 'force-dynamic'
|
|
* export async function GET() {
|
|
* const entries = await sitemap()
|
|
* const xml = buildSitemapXml(entries, { cssUrl: '/sitemap.css' })
|
|
* return new Response(xml, {
|
|
* headers: { 'Content-Type': 'application/xml; charset=utf-8' },
|
|
* })
|
|
* }
|
|
*
|
|
* Put sitemap.css in the project's /public and style the tags (see docs/seo.md
|
|
* for a starter). Note: <loc> is an XML tag, not <a href> — CSS can't make it a
|
|
* clickable link (some browsers auto-detect URLs); the win is readability, not
|
|
* clickability.
|
|
*
|
|
* NOTE: if you use this custom route, DON'T also keep app/sitemap.ts — pick one.
|
|
* Two sitemaps confuse crawlers.
|
|
*/
|
|
const esc = (s: string): string =>
|
|
s
|
|
.replace(/&/g, '&')
|
|
.replace(/</g, '<')
|
|
.replace(/>/g, '>')
|
|
.replace(/"/g, '"')
|
|
.replace(/'/g, ''')
|
|
|
|
const isoOf = (d: Date | string): string => (d instanceof Date ? d.toISOString() : String(d))
|
|
|
|
/** One child sitemap listed in a sitemap index. */
|
|
export type SitemapIndexItem = { lastModified?: Date | string; url: string }
|
|
|
|
/**
|
|
* Sitemap index (`<sitemapindex>`) — what /sitemap.xml returns once a site has
|
|
* more URLs than one file should hold. Each child carries the newest lastmod
|
|
* of its URLs, so crawlers re-read only the parts that changed.
|
|
*/
|
|
export function buildSitemapIndexXml(items: SitemapIndexItem[], opts: BuildSitemapXmlOptions = {}): string {
|
|
const stylesheet = opts.cssUrl ? `<?xml-stylesheet type="text/css" href="${esc(opts.cssUrl)}"?>\n` : ''
|
|
const body = items
|
|
.map((i) => {
|
|
const parts = [` <loc>${esc(i.url)}</loc>`]
|
|
if (i.lastModified) {parts.push(` <lastmod>${esc(isoOf(i.lastModified))}</lastmod>`)}
|
|
return ` <sitemap>\n${parts.join('\n')}\n </sitemap>`
|
|
})
|
|
.join('\n')
|
|
return (
|
|
`<?xml version="1.0" encoding="UTF-8"?>\n` +
|
|
stylesheet +
|
|
`<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n` +
|
|
body +
|
|
`\n</sitemapindex>`
|
|
)
|
|
}
|
|
|
|
export function buildSitemapXml(
|
|
entries: SitemapEntry[],
|
|
opts: BuildSitemapXmlOptions = {},
|
|
): string {
|
|
const { cssUrl } = opts
|
|
|
|
const urls = entries
|
|
.map((e) => {
|
|
const parts = [` <loc>${esc(e.url)}</loc>`]
|
|
if (e.lastModified) {
|
|
parts.push(` <lastmod>${esc(isoOf(e.lastModified))}</lastmod>`)
|
|
}
|
|
if (e.changeFrequency) {parts.push(` <changefreq>${e.changeFrequency}</changefreq>`)}
|
|
if (typeof e.priority === 'number') {parts.push(` <priority>${e.priority}</priority>`)}
|
|
const alternates = e.alternates?.languages
|
|
if (alternates) {
|
|
for (const [lang, href] of Object.entries(alternates)) {
|
|
if (typeof href === 'string') {
|
|
parts.push(
|
|
` <xhtml:link rel="alternate" hreflang="${esc(lang)}" href="${esc(href)}"/>`,
|
|
)
|
|
}
|
|
}
|
|
}
|
|
for (const img of e.images ?? []) {
|
|
parts.push(` <image:image><image:loc>${esc(img)}</image:loc></image:image>`)
|
|
}
|
|
return ` <url>\n${parts.join('\n')}\n </url>`
|
|
})
|
|
.join('\n')
|
|
|
|
const stylesheet = cssUrl ? `<?xml-stylesheet type="text/css" href="${esc(cssUrl)}"?>\n` : ''
|
|
|
|
return (
|
|
`<?xml version="1.0" encoding="UTF-8"?>\n` +
|
|
stylesheet +
|
|
`<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ` +
|
|
`xmlns:xhtml="http://www.w3.org/1999/xhtml" ` +
|
|
`xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">\n` +
|
|
urls +
|
|
`\n</urlset>`
|
|
)
|
|
}
|