From 42d843aeb1aff989276eb9144297703d62c8fef5 Mon Sep 17 00:00:00 2001 From: German Date: Fri, 13 Nov 2026 12:57:52 -0800 Subject: [PATCH] fix: advertise the operator documentation sitemap Add the independently published operator sitemap to robots.txt while preserving existing crawler rules and the root sitemap. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: a75e58d3-aa68-49f9-8213-70a354bfcc1b Signed-off-by: German --- scripts/generate-sitemap.mjs | 23 ++++++++++----------- tests/sitemapDiscovery.test.ts | 37 ++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 12 deletions(-) create mode 100644 tests/sitemapDiscovery.test.ts diff --git a/scripts/generate-sitemap.mjs b/scripts/generate-sitemap.mjs index 4c206da..934d59b 100644 --- a/scripts/generate-sitemap.mjs +++ b/scripts/generate-sitemap.mjs @@ -152,23 +152,22 @@ const sitemap = [ fs.writeFileSync(path.join(outDir, 'sitemap.xml'), sitemap); -// Advertise the sitemap from robots.txt. public/robots.txt (when present) has +// Advertise both independently published sites from robots.txt. public/robots.txt (when present) has // already been copied into out/ by the Next.js build; otherwise create a // minimal one. const robotsPath = path.join(outDir, 'robots.txt'); -const sitemapLine = `Sitemap: ${siteUrl}/sitemap.xml`; - -if (fs.existsSync(robotsPath)) { - const robots = fs.readFileSync(robotsPath, 'utf8'); - if (!robots.includes(sitemapLine)) { - fs.writeFileSync( - robotsPath, - `${robots.trimEnd()}\n\n${sitemapLine}\n`, - ); +let robots = fs.existsSync(robotsPath) + ? fs.readFileSync(robotsPath, 'utf8') + : 'User-agent: *\nAllow: /\n'; +for (const sitemapLine of [ + `Sitemap: ${siteUrl}/sitemap.xml`, + `Sitemap: ${siteUrl}/documentdb-kubernetes-operator/latest/sitemap.xml`, +]) { + if (!robots.split(/\r?\n/).includes(sitemapLine)) { + robots = `${robots.trimEnd()}\n\n${sitemapLine}\n`; } -} else { - fs.writeFileSync(robotsPath, `User-agent: *\nAllow: /\n\n${sitemapLine}\n`); } +fs.writeFileSync(robotsPath, robots); console.log( `Wrote out/sitemap.xml with ${pages.length} URLs (skipped ${noindexPagesSkipped} noindex ${ diff --git a/tests/sitemapDiscovery.test.ts b/tests/sitemapDiscovery.test.ts new file mode 100644 index 0000000..fb422da --- /dev/null +++ b/tests/sitemapDiscovery.test.ts @@ -0,0 +1,37 @@ +import { afterEach, describe, expect, it } from 'vitest'; +import { execFileSync } from 'node:child_process'; +import { mkdtemp, mkdir, readFile, rm, writeFile } from 'node:fs/promises'; +import { tmpdir } from 'node:os'; +import path from 'node:path'; + +const directories: string[] = []; +const generator = path.resolve('scripts/generate-sitemap.mjs'); +const rootSitemap = 'Sitemap: https://documentdb.io/sitemap.xml'; +const operatorSitemap = 'Sitemap: https://documentdb.io/documentdb-kubernetes-operator/latest/sitemap.xml'; + +afterEach(async () => { + await Promise.all(directories.splice(0).map((directory) => rm(directory, { recursive: true }))); +}); + +describe('operator sitemap discovery', () => { + it.each([false, true])('advertises both sitemaps with existing robots=%s', async (existingRobots) => { + const directory = await mkdtemp(path.join(tmpdir(), 'documentdb-sitemap-')); + directories.push(directory); + const out = path.join(directory, 'out'); + await mkdir(out); + await writeFile(path.join(out, 'index.html'), 'DocumentDB'); + if (existingRobots) { + await writeFile(path.join(out, 'robots.txt'), `User-agent: *\nDisallow: /private/\n\n${rootSitemap}\n`); + } + execFileSync(process.execPath, [generator], { cwd: directory }); + const robots = await readFile(path.join(out, 'robots.txt'), 'utf8'); + expect(robots.split('\n').filter((line) => line === rootSitemap)).toHaveLength(1); + expect(robots.split('\n').filter((line) => line === operatorSitemap)).toHaveLength(1); + if (existingRobots) { + expect(robots).toContain('Disallow: /private/'); + } + execFileSync(process.execPath, [generator], { cwd: directory }); + expect(await readFile(path.join(out, 'robots.txt'), 'utf8')).toBe(robots); + expect(await readFile(path.join(out, 'sitemap.xml'), 'utf8')).toContain('https://documentdb.io/'); + }); +});