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(v2): sitemap should respect the global trailingSlash config option. #5068

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import createSitemap from '../createSitemap';
import {DocusaurusConfig} from '@docusaurus/types';
import {EnumChangefreq} from 'sitemap';

describe('createSitemap', () => {
test('simple site', async () => {
Expand All @@ -16,7 +17,7 @@ describe('createSitemap', () => {
} as DocusaurusConfig,
['/', '/test'],
{
changefreq: 'daily',
changefreq: EnumChangefreq.DAILY,
priority: 0.7,
trailingSlash: false,
},
Expand All @@ -41,28 +42,87 @@ describe('createSitemap', () => {
} as DocusaurusConfig,
['/', '/404.html', '/mypage'],
{
changefreq: 'daily',
changefreq: EnumChangefreq.DAILY,
priority: 0.7,
trailingSlash: false,
},
);
expect(sitemap).not.toContain('404');
});

test('keep trailing slash unchanged', async () => {
const sitemap = await createSitemap(
{
url: 'https://example.com',
trailingSlash: undefined,
} as DocusaurusConfig,
['/', '/test', '/nested/test', '/nested/test2/'],
{
changefreq: EnumChangefreq.DAILY,
priority: 0.7,
},
);

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>');
});

test('add trailing slash', async () => {
const sitemap = await createSitemap(
{
url: 'https://example.com',
trailingSlash: true,
} as DocusaurusConfig,
['/', '/test'],
['/', '/test', '/nested/test', '/nested/test2/'],
{
changefreq: EnumChangefreq.DAILY,
priority: 0.7,
},
);

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>');
});

test('remove trailing slash', async () => {
const sitemap = await createSitemap(
{
url: 'https://example.com',
trailingSlash: false,
} as DocusaurusConfig,
['/', '/test', '/nested/test', '/nested/test2/'],
{
changefreq: EnumChangefreq.DAILY,
priority: 0.7,
},
);

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>');
});

test('add trailing slash (deprecated plugin option)', async () => {
const sitemap = await createSitemap(
{
url: 'https://example.com',
} as DocusaurusConfig,
['/', '/test', '/nested/test', '/nested/test2/'],
{
changefreq: 'daily',
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).not.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>');
});
});
4 changes: 2 additions & 2 deletions packages/docusaurus-plugin-sitemap/src/createSitemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default async function createSitemap(
if (!hostname) {
throw new Error('URL in docusaurus.config.js cannot be empty/undefined.');
}
const {changefreq, priority, trailingSlash} = options;
const {changefreq, priority} = options;

const sitemapStream = new SitemapStream({
hostname,
Expand All @@ -32,7 +32,7 @@ export default async function createSitemap(
if (options.trailingSlash) {
return addTrailingSlash(routePath);
} else {
return applyTrailingSlash(routePath, trailingSlash);
return applyTrailingSlash(routePath, siteConfig.trailingSlash);
}
}

Expand Down