Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions scripts/generate-sitemap.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ${
Expand Down
37 changes: 37 additions & 0 deletions tests/sitemapDiscovery.test.ts
Original file line number Diff line number Diff line change
@@ -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'), '<html><body>DocumentDB</body></html>');
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('<loc>https://documentdb.io/</loc>');
});
});
Loading