From 876ea06a1bda1e3f4581e0f3ce3a36728f454f5b Mon Sep 17 00:00:00 2001 From: Andre Alves Date: Tue, 16 Jan 2024 02:04:12 -0300 Subject: [PATCH 1/2] Fix base path formatting for ssr adapters --- .changeset/curly-seals-count.md | 5 +++ packages/integrations/sitemap/src/index.ts | 11 +++--- .../sitemap/test/base-path.test.js | 39 +++++++++++++++++++ 3 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 .changeset/curly-seals-count.md create mode 100644 packages/integrations/sitemap/test/base-path.test.js diff --git a/.changeset/curly-seals-count.md b/.changeset/curly-seals-count.md new file mode 100644 index 000000000000..2cec0e87b381 --- /dev/null +++ b/.changeset/curly-seals-count.md @@ -0,0 +1,5 @@ +--- +"@astrojs/sitemap": patch +--- + +Fix base path formatting for ssr adapters. diff --git a/packages/integrations/sitemap/src/index.ts b/packages/integrations/sitemap/src/index.ts index 79f6f9dfcb07..c254fb662bcf 100644 --- a/packages/integrations/sitemap/src/index.ts +++ b/packages/integrations/sitemap/src/index.ts @@ -107,11 +107,12 @@ const createPlugin = (options?: SitemapOptions): AstroIntegration => { */ if (r.pathname) { if (isStatusCodePage(r.pathname ?? r.route)) return urls; - /** - * remove the initial slash from relative pathname - * because `finalSiteUrl` always has trailing slash - */ - const fullPath = finalSiteUrl.pathname + r.generate(r.pathname).substring(1); + + // `finalSiteUrl` may end with a trailing slash + // or not because of base paths. + let fullPath = finalSiteUrl.pathname; + if (fullPath.endsWith('/')) fullPath += r.generate(r.pathname).substring(1); + else fullPath += r.generate(r.pathname); let newUrl = new URL(fullPath, finalSiteUrl).href; diff --git a/packages/integrations/sitemap/test/base-path.test.js b/packages/integrations/sitemap/test/base-path.test.js new file mode 100644 index 000000000000..a90f28c30275 --- /dev/null +++ b/packages/integrations/sitemap/test/base-path.test.js @@ -0,0 +1,39 @@ +import { loadFixture, readXML } from './test-utils.js'; +import { expect } from 'chai'; + +describe('URLs with base path', () => { + /** @type {import('./test-utils').Fixture} */ + let fixture; + + describe('using node adapter', () => { + before(async () => { + fixture = await loadFixture({ + root: './fixtures/ssr/', + base: '/base', + }); + await fixture.build(); + }); + + it('Base path is concatenated correctly', async () => { + const data = await readXML(fixture.readFile('/client/sitemap-0.xml')); + const urls = data.urlset.url; + expect(urls[0].loc[0]).to.equal('http://example.com/base/one/'); + }); + }); + + describe('static', () => { + before(async () => { + fixture = await loadFixture({ + root: './fixtures/static/', + base: '/base', + }); + await fixture.build(); + }); + + it('Base path is concatenated correctly', 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/123/'); + }); + }); +}); From 3e28ef78b5b1fb1e18ee9a0ac9f7c231a02d72aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Alves?= <71379045+andremralves@users.noreply.github.com> Date: Tue, 16 Jan 2024 13:50:15 -0300 Subject: [PATCH 2/2] Update .changeset/curly-seals-count.md Co-authored-by: Florian Lefebvre --- .changeset/curly-seals-count.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/curly-seals-count.md b/.changeset/curly-seals-count.md index 2cec0e87b381..f88178b8cea7 100644 --- a/.changeset/curly-seals-count.md +++ b/.changeset/curly-seals-count.md @@ -2,4 +2,4 @@ "@astrojs/sitemap": patch --- -Fix base path formatting for ssr adapters. +Fixes generated URLs when using a `base` with a SSR adapter