mirror of
https://github.com/facebook/docusaurus.git
synced 2025-08-06 10:20:09 +02:00
fix(sitemap): complete gracefully when all pages have noIndex meta (#7774)
This commit is contained in:
parent
665c3117af
commit
c3d2e0d30b
3 changed files with 47 additions and 5 deletions
|
@ -172,4 +172,39 @@ describe('createSitemap', () => {
|
||||||
|
|
||||||
expect(sitemap).not.toContain('/noindex');
|
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();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -18,7 +18,7 @@ export default async function createSitemap(
|
||||||
routesPaths: string[],
|
routesPaths: string[],
|
||||||
head: {[location: string]: HelmetServerState},
|
head: {[location: string]: HelmetServerState},
|
||||||
options: PluginOptions,
|
options: PluginOptions,
|
||||||
): Promise<string> {
|
): Promise<string | null> {
|
||||||
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.');
|
||||||
|
@ -27,9 +27,7 @@ export default async function createSitemap(
|
||||||
|
|
||||||
const ignoreMatcher = createMatcher(ignorePatterns);
|
const ignoreMatcher = createMatcher(ignorePatterns);
|
||||||
|
|
||||||
const sitemapStream = new SitemapStream({hostname});
|
const includedRoutes = routesPaths.filter((route) => {
|
||||||
|
|
||||||
function routeShouldBeIncluded(route: string) {
|
|
||||||
if (route.endsWith('404.html') || ignoreMatcher(route)) {
|
if (route.endsWith('404.html') || ignoreMatcher(route)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -40,9 +38,15 @@ export default async function createSitemap(
|
||||||
return !meta?.some(
|
return !meta?.some(
|
||||||
(tag) => tag.props.name === 'robots' && tag.props.content === 'noindex',
|
(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({
|
sitemapStream.write({
|
||||||
url: applyTrailingSlash(routePath, {
|
url: applyTrailingSlash(routePath, {
|
||||||
trailingSlash: siteConfig.trailingSlash,
|
trailingSlash: siteConfig.trailingSlash,
|
||||||
|
|
|
@ -30,6 +30,9 @@ export default function pluginSitemap(
|
||||||
head,
|
head,
|
||||||
options,
|
options,
|
||||||
);
|
);
|
||||||
|
if (!generatedSitemap) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Write sitemap file.
|
// Write sitemap file.
|
||||||
const sitemapPath = path.join(outDir, options.filename);
|
const sitemapPath = path.join(outDir, options.filename);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue