Skip to content

Commit 0e999db

Browse files
authored
fix(sitemap): skip the index entry when building page URLs (#1203)
1 parent ccd02e8 commit 0e999db

3 files changed

Lines changed: 51 additions & 1 deletion

File tree

‎.changeset/sitemap-skip-index.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@doc-kit/generator-react': patch
3+
---
4+
5+
Skip the `index` entry when building page URLs in the `sitemap` generator so the index page is only listed once under `indexURL`.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import assert from 'node:assert/strict';
2+
import { describe, it } from 'node:test';
3+
4+
import { setConfig } from '@doc-kit/core/utils/configuration/index.mjs';
5+
6+
import { generate } from '../generate.mjs';
7+
8+
const config = await setConfig({ target: ['sitemap'] });
9+
10+
config.sitemap.output = undefined;
11+
config.sitemap.baseURL = 'https://example.com';
12+
13+
const entry = (api, depth = 1) => ({
14+
api,
15+
path: `/${api}`,
16+
heading: { depth },
17+
});
18+
19+
const extractLocations = sitemap =>
20+
Array.from(sitemap.matchAll(/<loc>(.*?)<\/loc>/g), match => match[1]);
21+
22+
describe('sitemap', () => {
23+
it('lists depth-1 pages alongside the index URL', async () => {
24+
const sitemap = await generate([
25+
entry('fs', 1),
26+
entry('fs', 2),
27+
entry('http', 1),
28+
]);
29+
30+
assert.deepEqual(extractLocations(sitemap), [
31+
'https://example.com/fs.html',
32+
'https://example.com/http.html',
33+
'https://example.com/',
34+
]);
35+
});
36+
37+
it('does not list the index page twice when index.md is an input', async () => {
38+
const sitemap = await generate([entry('index', 1), entry('foo', 1)]);
39+
40+
assert.deepEqual(extractLocations(sitemap), [
41+
'https://example.com/foo.html',
42+
'https://example.com/',
43+
]);
44+
});
45+
});

‎packages/react/src/sitemap/generate.mjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export async function generate(entries) {
3030
const lastmod = new Date().toISOString().split('T')[0];
3131

3232
const apiPages = entries
33-
.filter(entry => entry.heading.depth === 1)
33+
.filter(entry => entry.heading.depth === 1 && entry.api !== 'index')
3434
.map(entry => createPageSitemapEntry(entry, config, lastmod));
3535

3636
const loc = populate(config.indexURL, config);

0 commit comments

Comments
 (0)