Fix #6618: sitemap urls generated without slash (#6658)

This commit is contained in:
André Alves 2023-04-04 10:13:49 -03:00 committed by GitHub
parent ce37bc77e0
commit 1ec1df1264
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 0 deletions

View file

@ -0,0 +1,5 @@
---
'@astrojs/sitemap': patch
---
Fix sitemap generation with a base path

View file

@ -79,6 +79,8 @@ const createPlugin = (options?: SitemapOptions): AstroIntegration => {
}
let pageUrls = pages.map((p) => {
if (p.pathname !== '' && !finalSiteUrl.pathname.endsWith('/'))
finalSiteUrl.pathname += '/';
const path = finalSiteUrl.pathname + p.pathname;
return new URL(path, finalSiteUrl).href;
});

View file

@ -59,6 +59,22 @@ describe('Trailing slash', () => {
const urls = data.urlset.url;
expect(urls[0].loc[0]).to.equal('http://example.com/one');
});
describe('with base path', () => {
before(async () => {
fixture = await loadFixture({
root: './fixtures/trailing-slash/',
trailingSlash: 'never',
base: '/base',
});
await fixture.build();
});
it('URLs do not end with trailing slash', async () => {
const data = await readXML(fixture.readFile('/sitemap-0.xml'));
const urls = data.urlset.url;
expect(urls[0].loc[0]).to.equal('http://example.com/base/one');
});
});
});
describe('trailingSlash: always', () => {
@ -75,5 +91,21 @@ describe('Trailing slash', () => {
const urls = data.urlset.url;
expect(urls[0].loc[0]).to.equal('http://example.com/one/');
});
describe('with base path', () => {
before(async () => {
fixture = await loadFixture({
root: './fixtures/trailing-slash/',
trailingSlash: 'always',
base: '/base',
});
await fixture.build();
});
it('URLs end with trailing slash', async () => {
const data = await readXML(fixture.readFile('/sitemap-0.xml'));
const urls = data.urlset.url;
expect(urls[0].loc[0]).to.equal('http://example.com/base/one/');
});
});
});
});