summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar André Alves <71379045+andremralves@users.noreply.github.com> 2023-04-04 10:13:49 -0300
committerGravatar GitHub <noreply@github.com> 2023-04-04 21:13:49 +0800
commit1ec1df12641290ec8b3a417a6284fd8d752c02bf (patch)
tree3ecd7dd88fc0e0e104abe560c42c0c0981f82938
parentce37bc77e0c838a705bc0b06ee48e3bc9d4e4768 (diff)
downloadastro-1ec1df12641290ec8b3a417a6284fd8d752c02bf.tar.gz
astro-1ec1df12641290ec8b3a417a6284fd8d752c02bf.tar.zst
astro-1ec1df12641290ec8b3a417a6284fd8d752c02bf.zip
Fix #6618: sitemap urls generated without slash (#6658)
-rw-r--r--.changeset/chatty-parrots-compare.md5
-rw-r--r--packages/integrations/sitemap/src/index.ts2
-rw-r--r--packages/integrations/sitemap/test/trailing-slash.test.js32
3 files changed, 39 insertions, 0 deletions
diff --git a/.changeset/chatty-parrots-compare.md b/.changeset/chatty-parrots-compare.md
new file mode 100644
index 000000000..f273c4ed1
--- /dev/null
+++ b/.changeset/chatty-parrots-compare.md
@@ -0,0 +1,5 @@
+---
+'@astrojs/sitemap': patch
+---
+
+Fix sitemap generation with a base path
diff --git a/packages/integrations/sitemap/src/index.ts b/packages/integrations/sitemap/src/index.ts
index 99448b9a1..e6e45ddd1 100644
--- a/packages/integrations/sitemap/src/index.ts
+++ b/packages/integrations/sitemap/src/index.ts
@@ -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;
});
diff --git a/packages/integrations/sitemap/test/trailing-slash.test.js b/packages/integrations/sitemap/test/trailing-slash.test.js
index b5b7dd6c1..a393fb9f1 100644
--- a/packages/integrations/sitemap/test/trailing-slash.test.js
+++ b/packages/integrations/sitemap/test/trailing-slash.test.js
@@ -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/');
+ });
+ });
});
});