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 #6618: sitemap urls generated without slash #6658

Merged
merged 3 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions .changeset/chatty-parrots-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/sitemap': patch
---

Fix sitemap generation with a base path
2 changes: 2 additions & 0 deletions packages/integrations/sitemap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ const createPlugin = (options?: SitemapOptions): AstroIntegration => {
}

let pageUrls = pages.map((p) => {
if (p.pathname != '' && !finalSiteUrl.pathname.endsWith('/'))
andremralves marked this conversation as resolved.
Show resolved Hide resolved
finalSiteUrl.pathname += '/';
const path = finalSiteUrl.pathname + p.pathname;
return new URL(path, finalSiteUrl).href;
});
Expand Down
32 changes: 32 additions & 0 deletions packages/integrations/sitemap/test/trailing-slash.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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/');
});
});
});
});