From c3d2e0d30b11117fa5986efd8adcd4b630adb822 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Thu, 14 Jul 2022 08:59:01 +0800 Subject: [PATCH] fix(sitemap): complete gracefully when all pages have noIndex meta (#7774) --- .../src/__tests__/createSitemap.test.ts | 35 +++++++++++++++++++ .../src/createSitemap.ts | 14 +++++--- .../docusaurus-plugin-sitemap/src/index.ts | 3 ++ 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/packages/docusaurus-plugin-sitemap/src/__tests__/createSitemap.test.ts b/packages/docusaurus-plugin-sitemap/src/__tests__/createSitemap.test.ts index b033a0cc7243..9d42f781baaa 100644 --- a/packages/docusaurus-plugin-sitemap/src/__tests__/createSitemap.test.ts +++ b/packages/docusaurus-plugin-sitemap/src/__tests__/createSitemap.test.ts @@ -172,4 +172,39 @@ describe('createSitemap', () => { expect(sitemap).not.toContain('/noindex'); }); + + it('does not generate anything for all pages with noindex', async () => { + const sitemap = await createSitemap( + { + url: 'https://example.com', + trailingSlash: false, + } as DocusaurusConfig, + ['/', '/noindex'], + { + '/': { + meta: { + // @ts-expect-error: bad lib def + toComponent: () => [ + React.createElement('meta', {name: 'robots', content: 'noindex'}), + ], + }, + }, + '/noindex': { + meta: { + // @ts-expect-error: bad lib def + toComponent: () => [ + React.createElement('meta', {name: 'robots', content: 'noindex'}), + ], + }, + }, + }, + { + changefreq: EnumChangefreq.DAILY, + priority: 0.7, + ignorePatterns: [], + }, + ); + + expect(sitemap).toBeNull(); + }); }); diff --git a/packages/docusaurus-plugin-sitemap/src/createSitemap.ts b/packages/docusaurus-plugin-sitemap/src/createSitemap.ts index 052dee692014..536f2ecfc6bd 100644 --- a/packages/docusaurus-plugin-sitemap/src/createSitemap.ts +++ b/packages/docusaurus-plugin-sitemap/src/createSitemap.ts @@ -18,7 +18,7 @@ export default async function createSitemap( routesPaths: string[], head: {[location: string]: HelmetServerState}, options: PluginOptions, -): Promise { +): Promise { const {url: hostname} = siteConfig; if (!hostname) { throw new Error('URL in docusaurus.config.js cannot be empty/undefined.'); @@ -27,9 +27,7 @@ export default async function createSitemap( const ignoreMatcher = createMatcher(ignorePatterns); - const sitemapStream = new SitemapStream({hostname}); - - function routeShouldBeIncluded(route: string) { + const includedRoutes = routesPaths.filter((route) => { if (route.endsWith('404.html') || ignoreMatcher(route)) { return false; } @@ -40,9 +38,15 @@ export default async function createSitemap( return !meta?.some( (tag) => tag.props.name === 'robots' && tag.props.content === 'noindex', ); + }); + + if (includedRoutes.length === 0) { + return null; } - routesPaths.filter(routeShouldBeIncluded).forEach((routePath) => + const sitemapStream = new SitemapStream({hostname}); + + includedRoutes.forEach((routePath) => sitemapStream.write({ url: applyTrailingSlash(routePath, { trailingSlash: siteConfig.trailingSlash, diff --git a/packages/docusaurus-plugin-sitemap/src/index.ts b/packages/docusaurus-plugin-sitemap/src/index.ts index 3044f1feff8b..fc71d6559080 100644 --- a/packages/docusaurus-plugin-sitemap/src/index.ts +++ b/packages/docusaurus-plugin-sitemap/src/index.ts @@ -30,6 +30,9 @@ export default function pluginSitemap( head, options, ); + if (!generatedSitemap) { + return; + } // Write sitemap file. const sitemapPath = path.join(outDir, options.filename);