Skip to content

Commit c3d2e0d

Browse files
authoredJul 14, 2022
fix(sitemap): complete gracefully when all pages have noIndex meta (#7774)
1 parent 665c311 commit c3d2e0d

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed
 

‎packages/docusaurus-plugin-sitemap/src/__tests__/createSitemap.test.ts

+35
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,39 @@ describe('createSitemap', () => {
172172

173173
expect(sitemap).not.toContain('/noindex');
174174
});
175+
176+
it('does not generate anything for all pages with noindex', async () => {
177+
const sitemap = await createSitemap(
178+
{
179+
url: 'https://example.com',
180+
trailingSlash: false,
181+
} as DocusaurusConfig,
182+
['/', '/noindex'],
183+
{
184+
'/': {
185+
meta: {
186+
// @ts-expect-error: bad lib def
187+
toComponent: () => [
188+
React.createElement('meta', {name: 'robots', content: 'noindex'}),
189+
],
190+
},
191+
},
192+
'/noindex': {
193+
meta: {
194+
// @ts-expect-error: bad lib def
195+
toComponent: () => [
196+
React.createElement('meta', {name: 'robots', content: 'noindex'}),
197+
],
198+
},
199+
},
200+
},
201+
{
202+
changefreq: EnumChangefreq.DAILY,
203+
priority: 0.7,
204+
ignorePatterns: [],
205+
},
206+
);
207+
208+
expect(sitemap).toBeNull();
209+
});
175210
});

‎packages/docusaurus-plugin-sitemap/src/createSitemap.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default async function createSitemap(
1818
routesPaths: string[],
1919
head: {[location: string]: HelmetServerState},
2020
options: PluginOptions,
21-
): Promise<string> {
21+
): Promise<string | null> {
2222
const {url: hostname} = siteConfig;
2323
if (!hostname) {
2424
throw new Error('URL in docusaurus.config.js cannot be empty/undefined.');
@@ -27,9 +27,7 @@ export default async function createSitemap(
2727

2828
const ignoreMatcher = createMatcher(ignorePatterns);
2929

30-
const sitemapStream = new SitemapStream({hostname});
31-
32-
function routeShouldBeIncluded(route: string) {
30+
const includedRoutes = routesPaths.filter((route) => {
3331
if (route.endsWith('404.html') || ignoreMatcher(route)) {
3432
return false;
3533
}
@@ -40,9 +38,15 @@ export default async function createSitemap(
4038
return !meta?.some(
4139
(tag) => tag.props.name === 'robots' && tag.props.content === 'noindex',
4240
);
41+
});
42+
43+
if (includedRoutes.length === 0) {
44+
return null;
4345
}
4446

45-
routesPaths.filter(routeShouldBeIncluded).forEach((routePath) =>
47+
const sitemapStream = new SitemapStream({hostname});
48+
49+
includedRoutes.forEach((routePath) =>
4650
sitemapStream.write({
4751
url: applyTrailingSlash(routePath, {
4852
trailingSlash: siteConfig.trailingSlash,

‎packages/docusaurus-plugin-sitemap/src/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ export default function pluginSitemap(
3030
head,
3131
options,
3232
);
33+
if (!generatedSitemap) {
34+
return;
35+
}
3336

3437
// Write sitemap file.
3538
const sitemapPath = path.join(outDir, options.filename);

0 commit comments

Comments
 (0)
Please sign in to comment.