|
| 1 | +// Next.js App Router sitemap. Emits one entry per (root URL, framework |
| 2 | +// variant) pair so search engines can crawl every framework-scoped doc |
| 3 | +// at its self-canonical URL. |
| 4 | +// |
| 5 | +// The expansion is: |
| 6 | +// - Root URL (/) |
| 7 | +// - Bare unscoped pages (/<slug>) from src/content/docs/**.mdx |
| 8 | +// - Framework-scoped pages (/<fw>/<slug>) bare slugs × every registered |
| 9 | +// integration, plus per-framework |
| 10 | +// override pages under |
| 11 | +// src/content/docs/integrations/ |
| 12 | +// - Reference (/reference/<slug>) from src/content/reference/ |
| 13 | +// - AG-UI (/ag-ui/<slug>) from src/content/ag-ui/ |
| 14 | +// |
| 15 | +// Each entry's `lastModified` is resolved via resolveLastModified — |
| 16 | +// frontmatter `lastmod` first, then file mtime, then `new Date()`. |
| 17 | + |
| 18 | +import type { MetadataRoute } from "next"; |
| 19 | +import { |
| 20 | + getAgUiPages, |
| 21 | + getBareDocsPages, |
| 22 | + getBaseUrl, |
| 23 | + getFrameworkOverridePages, |
| 24 | + getReferencePages, |
| 25 | + resolveLastModified, |
| 26 | +} from "@/lib/sitemap-helpers"; |
| 27 | +import { getDocsFolder, getIntegrations } from "@/lib/registry"; |
| 28 | + |
| 29 | +export default function sitemap(): MetadataRoute.Sitemap { |
| 30 | + const baseUrl = getBaseUrl(); |
| 31 | + const now = new Date(); |
| 32 | + const entries: MetadataRoute.Sitemap = []; |
| 33 | + |
| 34 | + // 1. Root / overview. |
| 35 | + entries.push({ |
| 36 | + url: `${baseUrl}/`, |
| 37 | + lastModified: now, |
| 38 | + }); |
| 39 | + |
| 40 | + // 2. Bare unscoped docs and 3. framework-scoped variants. Each bare |
| 41 | + // slug generates one bare entry plus N framework-scoped entries — one |
| 42 | + // per registered integration. Per-framework override pages are added |
| 43 | + // alongside (deduped against the bare × framework cross-product). |
| 44 | + const bareDocs = getBareDocsPages(); |
| 45 | + const integrations = getIntegrations(); |
| 46 | + |
| 47 | + // Track every framework-scoped URL we've already emitted so the |
| 48 | + // override loop below can skip duplicates without scanning the array. |
| 49 | + const seenFrameworkUrls = new Set<string>(); |
| 50 | + |
| 51 | + for (const { slug, filePath } of bareDocs) { |
| 52 | + const lastModified = resolveLastModified(filePath); |
| 53 | + entries.push({ |
| 54 | + url: `${baseUrl}/${slug}`, |
| 55 | + lastModified, |
| 56 | + }); |
| 57 | + for (const integration of integrations) { |
| 58 | + const url = `${baseUrl}/${integration.slug}/${slug}`; |
| 59 | + seenFrameworkUrls.add(url); |
| 60 | + entries.push({ url, lastModified }); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + // Framework landing pages: /<framework> on its own. |
| 65 | + for (const integration of integrations) { |
| 66 | + const url = `${baseUrl}/${integration.slug}`; |
| 67 | + seenFrameworkUrls.add(url); |
| 68 | + entries.push({ url, lastModified: now }); |
| 69 | + } |
| 70 | + |
| 71 | + // Per-framework override pages — topics that only exist under |
| 72 | + // integrations/<folder>/. These are addressable as /<framework>/<slug> |
| 73 | + // and aren't covered by the bare × framework cross-product above. |
| 74 | + for (const integration of integrations) { |
| 75 | + const folder = getDocsFolder(integration.slug); |
| 76 | + for (const { slug, filePath } of getFrameworkOverridePages(folder)) { |
| 77 | + const url = `${baseUrl}/${integration.slug}/${slug}`; |
| 78 | + if (seenFrameworkUrls.has(url)) continue; |
| 79 | + seenFrameworkUrls.add(url); |
| 80 | + entries.push({ |
| 81 | + url, |
| 82 | + lastModified: resolveLastModified(filePath), |
| 83 | + }); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + // 4. Reference docs. |
| 88 | + for (const { slug, filePath } of getReferencePages()) { |
| 89 | + entries.push({ |
| 90 | + url: `${baseUrl}/reference/${slug}`, |
| 91 | + lastModified: resolveLastModified(filePath), |
| 92 | + }); |
| 93 | + } |
| 94 | + // Reference index. |
| 95 | + entries.push({ url: `${baseUrl}/reference`, lastModified: now }); |
| 96 | + |
| 97 | + // 5. AG-UI. |
| 98 | + for (const { slug, filePath } of getAgUiPages()) { |
| 99 | + entries.push({ |
| 100 | + url: `${baseUrl}/ag-ui/${slug}`, |
| 101 | + lastModified: resolveLastModified(filePath), |
| 102 | + }); |
| 103 | + } |
| 104 | + // AG-UI overview landing. |
| 105 | + entries.push({ url: `${baseUrl}/ag-ui`, lastModified: now }); |
| 106 | + |
| 107 | + return entries; |
| 108 | +} |
0 commit comments