fix(v2): don't include 404 page in sitemaps (#2616)

* fix(v2): don't include 404 page in sitemaps

Signed-off-by: Reece Dunham <me@rdil.rocks>

* fix: improve implementation as requested

Signed-off-by: Reece Dunham <me@rdil.rocks>

* Fix issue with baseUrls
This commit is contained in:
Reece Dunham 2020-04-17 23:27:11 -04:00 committed by GitHub
parent 14f4ef875a
commit 614adca45f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 10 deletions

View file

@ -30,7 +30,22 @@ describe('createSitemap', () => {
expect(() => { expect(() => {
createSitemap({} as any, [], {} as any); createSitemap({} as any, [], {} as any);
}).toThrowErrorMatchingInlineSnapshot( }).toThrowErrorMatchingInlineSnapshot(
`"Url in docusaurus.config.js cannot be empty/undefined"`, `"url in docusaurus.config.js cannot be empty/undefined"`,
); );
}); });
test('exclusion of 404 page', () => {
const sitemap = createSitemap(
{
url: 'https://example.com',
} as DocusaurusConfig,
['/', '/404.html', '/mypage'],
{
cacheTime: 600,
changefreq: 'daily',
priority: 0.7,
},
);
expect(sitemap.toString()).not.toContain('404');
});
}); });

View file

@ -16,17 +16,19 @@ export default function createSitemap(
) { ) {
const {url: hostname} = siteConfig; const {url: hostname} = siteConfig;
if (!hostname) { if (!hostname) {
throw new Error('Url in docusaurus.config.js cannot be empty/undefined'); throw new Error('url in docusaurus.config.js cannot be empty/undefined');
} }
const urls = routesPaths.map( const urls = routesPaths
(routesPath) => .filter((route: string) => !route.endsWith('404.html'))
({ .map(
url: routesPath, (routesPath) =>
changefreq: options.changefreq, ({
priority: options.priority, url: routesPath,
} as SitemapItemOptions), changefreq: options.changefreq,
); priority: options.priority,
} as SitemapItemOptions),
);
return sitemap.createSitemap({ return sitemap.createSitemap({
hostname, hostname,