mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-17 10:12:25 +02:00
feat(v2): add an option to toggle trailing slash for urls in sitemap (#3426)
This commit is contained in:
parent
3638b1651a
commit
d844ff6107
6 changed files with 32 additions and 1 deletions
|
@ -19,6 +19,7 @@ describe('createSitemap', () => {
|
||||||
cacheTime: 600,
|
cacheTime: 600,
|
||||||
changefreq: 'daily',
|
changefreq: 'daily',
|
||||||
priority: 0.7,
|
priority: 0.7,
|
||||||
|
trailingSlash: false,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
expect(sitemap.toString()).toContain(
|
expect(sitemap.toString()).toContain(
|
||||||
|
@ -44,8 +45,31 @@ describe('createSitemap', () => {
|
||||||
cacheTime: 600,
|
cacheTime: 600,
|
||||||
changefreq: 'daily',
|
changefreq: 'daily',
|
||||||
priority: 0.7,
|
priority: 0.7,
|
||||||
|
trailingSlash: false,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
expect(sitemap.toString()).not.toContain('404');
|
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>',
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -29,6 +29,7 @@ describe('normalizeSitemapPluginOptions', () => {
|
||||||
cacheTime: 300,
|
cacheTime: 300,
|
||||||
changefreq: 'yearly',
|
changefreq: 'yearly',
|
||||||
priority: 0.9,
|
priority: 0.9,
|
||||||
|
trailingSlash: false,
|
||||||
};
|
};
|
||||||
const {value} = await PluginOptionSchema.validate(userOptions);
|
const {value} = await PluginOptionSchema.validate(userOptions);
|
||||||
expect(value).toEqual(userOptions);
|
expect(value).toEqual(userOptions);
|
||||||
|
|
|
@ -24,7 +24,9 @@ export default function createSitemap(
|
||||||
.map(
|
.map(
|
||||||
(routesPath) =>
|
(routesPath) =>
|
||||||
({
|
({
|
||||||
url: routesPath,
|
url: `${routesPath}${
|
||||||
|
options.trailingSlash && routesPath !== '/' ? '/' : ''
|
||||||
|
}`,
|
||||||
changefreq: options.changefreq,
|
changefreq: options.changefreq,
|
||||||
priority: options.priority,
|
priority: options.priority,
|
||||||
} as SitemapItemOptions),
|
} as SitemapItemOptions),
|
||||||
|
|
|
@ -11,6 +11,7 @@ export const DEFAULT_OPTIONS: Required<PluginOptions> = {
|
||||||
cacheTime: 600 * 1000, // 600 sec - cache purge period.
|
cacheTime: 600 * 1000, // 600 sec - cache purge period.
|
||||||
changefreq: 'weekly',
|
changefreq: 'weekly',
|
||||||
priority: 0.5,
|
priority: 0.5,
|
||||||
|
trailingSlash: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
export const PluginOptionSchema = Joi.object({
|
export const PluginOptionSchema = Joi.object({
|
||||||
|
@ -19,4 +20,5 @@ export const PluginOptionSchema = Joi.object({
|
||||||
.valid('always', 'hourly', 'daily', 'weekly', 'monthly', 'yearly', 'never')
|
.valid('always', 'hourly', 'daily', 'weekly', 'monthly', 'yearly', 'never')
|
||||||
.default(DEFAULT_OPTIONS.changefreq),
|
.default(DEFAULT_OPTIONS.changefreq),
|
||||||
priority: Joi.number().min(0).max(1).default(DEFAULT_OPTIONS.priority),
|
priority: Joi.number().min(0).max(1).default(DEFAULT_OPTIONS.priority),
|
||||||
|
trailingSlash: Joi.bool().default(false),
|
||||||
});
|
});
|
||||||
|
|
|
@ -9,4 +9,5 @@ export interface PluginOptions {
|
||||||
cacheTime?: number;
|
cacheTime?: number;
|
||||||
changefreq?: string;
|
changefreq?: string;
|
||||||
priority?: number;
|
priority?: number;
|
||||||
|
trailingSlash?: boolean;
|
||||||
}
|
}
|
||||||
|
|
|
@ -517,6 +517,7 @@ module.exports = {
|
||||||
cacheTime: 600 * 1000, // 600 sec - cache purge period
|
cacheTime: 600 * 1000, // 600 sec - cache purge period
|
||||||
changefreq: 'weekly',
|
changefreq: 'weekly',
|
||||||
priority: 0.5,
|
priority: 0.5,
|
||||||
|
trailingSlash: false,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue