Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sitemap): complete gracefully when all pages have noIndex meta #7774

Merged
merged 1 commit into from Jul 14, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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();
});
});
14 changes: 9 additions & 5 deletions packages/docusaurus-plugin-sitemap/src/createSitemap.ts
Expand Up @@ -18,7 +18,7 @@ export default async function createSitemap(
routesPaths: string[],
head: {[location: string]: HelmetServerState},
options: PluginOptions,
): Promise<string> {
): Promise<string | null> {
const {url: hostname} = siteConfig;
if (!hostname) {
throw new Error('URL in docusaurus.config.js cannot be empty/undefined.');
Expand All @@ -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;
}
Expand All @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions packages/docusaurus-plugin-sitemap/src/index.ts
Expand Up @@ -30,6 +30,9 @@ export default function pluginSitemap(
head,
options,
);
if (!generatedSitemap) {
return;
}

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