From 135960e3b4575bb472c7488bd8f2ec9dd4edc736 Mon Sep 17 00:00:00 2001 From: Piotr Losiak Date: Sun, 4 Feb 2024 22:18:45 +0100 Subject: [PATCH 1/3] fix(sitemap): url when rest parameter is used in page file names --- .changeset/sour-ties-sparkle.md | 5 ++++ packages/integrations/sitemap/src/index.ts | 2 ++ .../sitemap/test/dynamic-path.test.js | 24 +++++++++++++++++++ .../test/fixtures/dynamic/astro.config.mjs | 7 ++++++ .../test/fixtures/dynamic/package.json | 9 +++++++ .../dynamic/src/pages/[...slug].astro | 21 ++++++++++++++++ pnpm-lock.yaml | 9 +++++++ 7 files changed, 77 insertions(+) create mode 100644 .changeset/sour-ties-sparkle.md create mode 100644 packages/integrations/sitemap/test/dynamic-path.test.js create mode 100644 packages/integrations/sitemap/test/fixtures/dynamic/astro.config.mjs create mode 100644 packages/integrations/sitemap/test/fixtures/dynamic/package.json create mode 100644 packages/integrations/sitemap/test/fixtures/dynamic/src/pages/[...slug].astro diff --git a/.changeset/sour-ties-sparkle.md b/.changeset/sour-ties-sparkle.md new file mode 100644 index 000000000000..f96912628303 --- /dev/null +++ b/.changeset/sour-ties-sparkle.md @@ -0,0 +1,5 @@ +--- +"@astrojs/sitemap": patch +--- + +Fix generating urls while rest parameter in page file names is used diff --git a/packages/integrations/sitemap/src/index.ts b/packages/integrations/sitemap/src/index.ts index c254fb662bcf..b75c816a962a 100644 --- a/packages/integrations/sitemap/src/index.ts +++ b/packages/integrations/sitemap/src/index.ts @@ -94,6 +94,8 @@ const createPlugin = (options?: SitemapOptions): AstroIntegration => { .map((p) => { if (p.pathname !== '' && !finalSiteUrl.pathname.endsWith('/')) finalSiteUrl.pathname += '/'; + if (p.pathname.startsWith('/')) + p.pathname = p.pathname.slice(1); const fullPath = finalSiteUrl.pathname + p.pathname; return new URL(fullPath, finalSiteUrl).href; }); diff --git a/packages/integrations/sitemap/test/dynamic-path.test.js b/packages/integrations/sitemap/test/dynamic-path.test.js new file mode 100644 index 000000000000..4f84af39e7af --- /dev/null +++ b/packages/integrations/sitemap/test/dynamic-path.test.js @@ -0,0 +1,24 @@ +import {before, describe, it} from "node:test"; +import {loadFixture, readXML} from "./test-utils.js"; +import assert from "node:assert/strict"; + +describe('Dynamic with rest parameter', () => { + /** @type {import('./test-utils.js').Fixture} */ + let fixture; + + before(async () => { + fixture = await loadFixture({ + root: './fixtures/dynamic', + }); + await fixture.build(); + }); + + it('Should generate correct urls', async () => { + const data = await readXML(fixture.readFile('/sitemap-0.xml')); + const urls = data.urlset.url.map((url) => url.loc[0]); + + assert.ok(urls.includes('http://example.com/')); + assert.ok(urls.includes('http://example.com/blog/')); + assert.ok(urls.includes('http://example.com/test/')); + }); +}) diff --git a/packages/integrations/sitemap/test/fixtures/dynamic/astro.config.mjs b/packages/integrations/sitemap/test/fixtures/dynamic/astro.config.mjs new file mode 100644 index 000000000000..7d02e26caf3a --- /dev/null +++ b/packages/integrations/sitemap/test/fixtures/dynamic/astro.config.mjs @@ -0,0 +1,7 @@ +import { defineConfig } from 'astro/config'; +import sitemap from '@astrojs/sitemap'; + +export default defineConfig({ + integrations: [sitemap()], + site: 'http://example.com' +}) diff --git a/packages/integrations/sitemap/test/fixtures/dynamic/package.json b/packages/integrations/sitemap/test/fixtures/dynamic/package.json new file mode 100644 index 000000000000..1eac19a1bc3d --- /dev/null +++ b/packages/integrations/sitemap/test/fixtures/dynamic/package.json @@ -0,0 +1,9 @@ +{ + "name": "@test/sitemap-dynamic", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*", + "@astrojs/sitemap": "workspace:*" + } +} diff --git a/packages/integrations/sitemap/test/fixtures/dynamic/src/pages/[...slug].astro b/packages/integrations/sitemap/test/fixtures/dynamic/src/pages/[...slug].astro new file mode 100644 index 000000000000..9622cb374c20 --- /dev/null +++ b/packages/integrations/sitemap/test/fixtures/dynamic/src/pages/[...slug].astro @@ -0,0 +1,21 @@ +--- +export async function getStaticPaths() { + return [ + { + params: { + slug: undefined, + } + }, + { + params: { + slug: '/blog' + } + }, + { + params: { + slug: '/test' + } + } + ]; +} +--- diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 193bb1ae735c..135ec8ff19fe 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -4577,6 +4577,15 @@ importers: specifier: 0.6.2 version: 0.6.2 + packages/integrations/sitemap/test/fixtures/dynamic: + dependencies: + '@astrojs/sitemap': + specifier: workspace:* + version: link:../../.. + astro: + specifier: workspace:* + version: link:../../../../../astro + packages/integrations/sitemap/test/fixtures/ssr: dependencies: '@astrojs/sitemap': From 0548de4a61448c7cbc5759a6c21b4b4741232055 Mon Sep 17 00:00:00 2001 From: Piotr Losiak <47902696+moose96@users.noreply.github.com> Date: Mon, 5 Feb 2024 10:11:39 +0100 Subject: [PATCH 2/3] Update .changeset/sour-ties-sparkle.md Co-authored-by: Florian Lefebvre --- .changeset/sour-ties-sparkle.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/sour-ties-sparkle.md b/.changeset/sour-ties-sparkle.md index f96912628303..ef1038a2c6fe 100644 --- a/.changeset/sour-ties-sparkle.md +++ b/.changeset/sour-ties-sparkle.md @@ -2,4 +2,4 @@ "@astrojs/sitemap": patch --- -Fix generating urls while rest parameter in page file names is used +Fixes URLs generation for routes using rest parameter From 456b353db00b2a20887b9b06c85e8e9777d991ec Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Mon, 19 Feb 2024 11:59:40 +0000 Subject: [PATCH 3/3] Apply suggestions from code review --- .changeset/sour-ties-sparkle.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/sour-ties-sparkle.md b/.changeset/sour-ties-sparkle.md index ef1038a2c6fe..17088feae350 100644 --- a/.changeset/sour-ties-sparkle.md +++ b/.changeset/sour-ties-sparkle.md @@ -2,4 +2,4 @@ "@astrojs/sitemap": patch --- -Fixes URLs generation for routes using rest parameter +Fixes URL generation for routes that rest parameters and start with `/`