feat(v2): add an option to toggle trailing slash for urls in sitemap (#3426)

This commit is contained in:
Méril 2020-09-09 14:48:47 +01:00 committed by GitHub
parent 3638b1651a
commit d844ff6107
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 32 additions and 1 deletions

View file

@ -19,6 +19,7 @@ describe('createSitemap', () => {
cacheTime: 600,
changefreq: 'daily',
priority: 0.7,
trailingSlash: false,
},
);
expect(sitemap.toString()).toContain(
@ -44,8 +45,31 @@ describe('createSitemap', () => {
cacheTime: 600,
changefreq: 'daily',
priority: 0.7,
trailingSlash: false,
},
);
expect(sitemap.toString()).not.toContain('404');
});
test('add trailing slash', () => {
const sitemap = createSitemap(
{
url: 'https://example.com',
} as DocusaurusConfig,
['/', '/test'],
{
cacheTime: 600,
changefreq: 'daily',
priority: 0.7,
trailingSlash: true,
},
);
expect(sitemap.toString()).toContain(
'<loc>https://example.com/test/</loc>',
);
expect(sitemap.toString()).not.toContain(
'<loc>https://example.com/test</loc>',
);
});
});

View file

@ -29,6 +29,7 @@ describe('normalizeSitemapPluginOptions', () => {
cacheTime: 300,
changefreq: 'yearly',
priority: 0.9,
trailingSlash: false,
};
const {value} = await PluginOptionSchema.validate(userOptions);
expect(value).toEqual(userOptions);

View file

@ -24,7 +24,9 @@ export default function createSitemap(
.map(
(routesPath) =>
({
url: routesPath,
url: `${routesPath}${
options.trailingSlash && routesPath !== '/' ? '/' : ''
}`,
changefreq: options.changefreq,
priority: options.priority,
} as SitemapItemOptions),

View file

@ -11,6 +11,7 @@ export const DEFAULT_OPTIONS: Required<PluginOptions> = {
cacheTime: 600 * 1000, // 600 sec - cache purge period.
changefreq: 'weekly',
priority: 0.5,
trailingSlash: false,
};
export const PluginOptionSchema = Joi.object({
@ -19,4 +20,5 @@ export const PluginOptionSchema = Joi.object({
.valid('always', 'hourly', 'daily', 'weekly', 'monthly', 'yearly', 'never')
.default(DEFAULT_OPTIONS.changefreq),
priority: Joi.number().min(0).max(1).default(DEFAULT_OPTIONS.priority),
trailingSlash: Joi.bool().default(false),
});

View file

@ -9,4 +9,5 @@ export interface PluginOptions {
cacheTime?: number;
changefreq?: string;
priority?: number;
trailingSlash?: boolean;
}

View file

@ -517,6 +517,7 @@ module.exports = {
cacheTime: 600 * 1000, // 600 sec - cache purge period
changefreq: 'weekly',
priority: 0.5,
trailingSlash: false,
},
],
],