mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-25 14:07:50 +02:00
feat(sitemap): remove trailingSlash option; respect noIndex config (#6248)
This commit is contained in:
parent
37b70e3ab4
commit
8fe1ddf46a
7 changed files with 23 additions and 57 deletions
|
@ -19,7 +19,6 @@ describe('createSitemap', () => {
|
|||
{
|
||||
changefreq: EnumChangefreq.DAILY,
|
||||
priority: 0.7,
|
||||
trailingSlash: false,
|
||||
},
|
||||
);
|
||||
expect(sitemap).toContain(
|
||||
|
@ -43,7 +42,6 @@ describe('createSitemap', () => {
|
|||
{
|
||||
changefreq: EnumChangefreq.DAILY,
|
||||
priority: 0.7,
|
||||
trailingSlash: false,
|
||||
},
|
||||
);
|
||||
expect(sitemap).not.toContain('404');
|
||||
|
@ -105,23 +103,4 @@ describe('createSitemap', () => {
|
|||
expect(sitemap).toContain('<loc>https://example.com/nested/test</loc>');
|
||||
expect(sitemap).toContain('<loc>https://example.com/nested/test2</loc>');
|
||||
});
|
||||
|
||||
test('add trailing slash (deprecated plugin option)', async () => {
|
||||
const sitemap = await createSitemap(
|
||||
{
|
||||
url: 'https://example.com',
|
||||
} as DocusaurusConfig,
|
||||
['/', '/test', '/nested/test', '/nested/test2/'],
|
||||
{
|
||||
changefreq: EnumChangefreq.DAILY,
|
||||
priority: 0.7,
|
||||
trailingSlash: true,
|
||||
},
|
||||
);
|
||||
|
||||
expect(sitemap).toContain('<loc>https://example.com/</loc>');
|
||||
expect(sitemap).toContain('<loc>https://example.com/test/</loc>');
|
||||
expect(sitemap).toContain('<loc>https://example.com/nested/test/</loc>');
|
||||
expect(sitemap).toContain('<loc>https://example.com/nested/test2/</loc>');
|
||||
});
|
||||
});
|
||||
|
|
|
@ -28,15 +28,9 @@ describe('normalizeSitemapPluginOptions', () => {
|
|||
const userOptions = {
|
||||
changefreq: 'yearly',
|
||||
priority: 0.9,
|
||||
trailingSlash: false,
|
||||
};
|
||||
const {value, warning} = await PluginOptionSchema.validate(userOptions);
|
||||
const {value} = await PluginOptionSchema.validate(userOptions);
|
||||
expect(value).toEqual(userOptions);
|
||||
|
||||
expect(warning?.details?.length).toEqual(1);
|
||||
expect(warning?.details[0].message).toMatchInlineSnapshot(
|
||||
`"Option \\"trailingSlash\\" of the sitemap plugin is deprecated: Please use the new Docusaurus global trailingSlash config instead, and the sitemaps plugin will use it."`,
|
||||
);
|
||||
});
|
||||
|
||||
test('should reject out-of-range priority inputs', () => {
|
||||
|
|
|
@ -8,7 +8,6 @@
|
|||
import {SitemapStream, streamToPromise} from 'sitemap';
|
||||
import type {Options} from '@docusaurus/plugin-sitemap';
|
||||
import type {DocusaurusConfig} from '@docusaurus/types';
|
||||
import {addTrailingSlash} from '@docusaurus/utils';
|
||||
import {applyTrailingSlash} from '@docusaurus/utils-common';
|
||||
|
||||
export default async function createSitemap(
|
||||
|
@ -22,28 +21,16 @@ export default async function createSitemap(
|
|||
}
|
||||
const {changefreq, priority} = options;
|
||||
|
||||
const sitemapStream = new SitemapStream({
|
||||
hostname,
|
||||
});
|
||||
|
||||
function applySitemapTrailingSlash(routePath: string): string {
|
||||
// kept for retrocompatibility
|
||||
// TODO remove deprecated trailingSlash option before 2022
|
||||
if (options.trailingSlash) {
|
||||
return addTrailingSlash(routePath);
|
||||
} else {
|
||||
return applyTrailingSlash(routePath, {
|
||||
trailingSlash: siteConfig.trailingSlash,
|
||||
baseUrl: siteConfig.baseUrl,
|
||||
});
|
||||
}
|
||||
}
|
||||
const sitemapStream = new SitemapStream({hostname});
|
||||
|
||||
routesPaths
|
||||
.filter((route) => !route.endsWith('404.html'))
|
||||
.map((routePath) =>
|
||||
.forEach((routePath) =>
|
||||
sitemapStream.write({
|
||||
url: applySitemapTrailingSlash(routePath),
|
||||
url: applyTrailingSlash(routePath, {
|
||||
trailingSlash: siteConfig.trailingSlash,
|
||||
baseUrl: siteConfig.baseUrl,
|
||||
}),
|
||||
changefreq,
|
||||
priority,
|
||||
}),
|
||||
|
@ -51,9 +38,7 @@ export default async function createSitemap(
|
|||
|
||||
sitemapStream.end();
|
||||
|
||||
const generatedSitemap = await streamToPromise(sitemapStream).then((sm) =>
|
||||
sm.toString(),
|
||||
);
|
||||
const generatedSitemap = (await streamToPromise(sitemapStream)).toString();
|
||||
|
||||
return generatedSitemap;
|
||||
}
|
||||
|
|
|
@ -26,6 +26,9 @@ export default function pluginSitemap(
|
|||
name: 'docusaurus-plugin-sitemap',
|
||||
|
||||
async postBuild({siteConfig, routesPaths, outDir}: Props) {
|
||||
if (siteConfig.noIndex) {
|
||||
return;
|
||||
}
|
||||
// Generate sitemap.
|
||||
const generatedSitemap = await createSitemap(
|
||||
siteConfig,
|
||||
|
|
|
@ -10,5 +10,4 @@ import type {EnumChangefreq} from 'sitemap';
|
|||
export type Options = {
|
||||
changefreq?: EnumChangefreq;
|
||||
priority?: number;
|
||||
trailingSlash?: boolean;
|
||||
};
|
||||
|
|
|
@ -12,7 +12,6 @@ import type {Options} from '@docusaurus/plugin-sitemap';
|
|||
export const DEFAULT_OPTIONS: Required<Options> = {
|
||||
changefreq: EnumChangefreq.WEEKLY,
|
||||
priority: 0.5,
|
||||
trailingSlash: false,
|
||||
};
|
||||
|
||||
export const PluginOptionSchema = Joi.object({
|
||||
|
@ -25,10 +24,8 @@ export const PluginOptionSchema = Joi.object({
|
|||
.valid(...Object.values(EnumChangefreq))
|
||||
.default(DEFAULT_OPTIONS.changefreq),
|
||||
priority: Joi.number().min(0).max(1).default(DEFAULT_OPTIONS.priority),
|
||||
trailingSlash: Joi.bool().default(false).warning('deprecate.error', {
|
||||
msg: 'Please use the new Docusaurus global trailingSlash config instead, and the sitemaps plugin will use it.',
|
||||
trailingSlash: Joi.forbidden().messages({
|
||||
'any.unknown':
|
||||
'Please use the new Docusaurus global trailingSlash config instead, and the sitemaps plugin will use it.',
|
||||
}),
|
||||
}).messages({
|
||||
'deprecate.error':
|
||||
'Option {#label} of the sitemap plugin is deprecated: {#msg}',
|
||||
});
|
||||
|
|
|
@ -42,6 +42,15 @@ Accepted fields:
|
|||
|
||||
</APITable>
|
||||
|
||||
:::info
|
||||
|
||||
This plugin also respects some site config:
|
||||
|
||||
- [`noIndex`](../docusaurus.config.js.md#noindex): results in no sitemap generated
|
||||
- [`trailingSlash`](../docusaurus.config.js.md#trailing-slash): determines if the URLs in the sitemap have trailing slashes
|
||||
|
||||
:::
|
||||
|
||||
### Example configuration {#ex-config}
|
||||
|
||||
You can configure this plugin through preset options or plugin options.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue