summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Piotr Losiak <47902696+moose96@users.noreply.github.com> 2024-02-21 08:43:22 +0100
committerGravatar GitHub <noreply@github.com> 2024-02-21 07:43:22 +0000
commitec7d2ebbd96b8c2dfdadaf076bbf7953007536ed (patch)
tree8635d90dde451e9171e1c5a64181a30c7b215fa9
parent9001d063aeb19677ba556c2890e4b8b9513fd6de (diff)
downloadastro-ec7d2ebbd96b8c2dfdadaf076bbf7953007536ed.tar.gz
astro-ec7d2ebbd96b8c2dfdadaf076bbf7953007536ed.tar.zst
astro-ec7d2ebbd96b8c2dfdadaf076bbf7953007536ed.zip
fix(sitemap): url when rest parameter is used in page file names (#9975)
* fix(sitemap): url when rest parameter is used in page file names * Update .changeset/sour-ties-sparkle.md Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev> * Apply suggestions from code review --------- Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev> Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
-rw-r--r--.changeset/sour-ties-sparkle.md5
-rw-r--r--packages/integrations/sitemap/src/index.ts2
-rw-r--r--packages/integrations/sitemap/test/dynamic-path.test.js24
-rw-r--r--packages/integrations/sitemap/test/fixtures/dynamic/astro.config.mjs7
-rw-r--r--packages/integrations/sitemap/test/fixtures/dynamic/package.json9
-rw-r--r--packages/integrations/sitemap/test/fixtures/dynamic/src/pages/[...slug].astro21
-rw-r--r--pnpm-lock.yaml9
7 files changed, 77 insertions, 0 deletions
diff --git a/.changeset/sour-ties-sparkle.md b/.changeset/sour-ties-sparkle.md
new file mode 100644
index 000000000..17088feae
--- /dev/null
+++ b/.changeset/sour-ties-sparkle.md
@@ -0,0 +1,5 @@
+---
+"@astrojs/sitemap": patch
+---
+
+Fixes URL generation for routes that rest parameters and start with `/`
diff --git a/packages/integrations/sitemap/src/index.ts b/packages/integrations/sitemap/src/index.ts
index 4c049df91..4d280112b 100644
--- a/packages/integrations/sitemap/src/index.ts
+++ b/packages/integrations/sitemap/src/index.ts
@@ -98,6 +98,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 000000000..4f84af39e
--- /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 000000000..7d02e26ca
--- /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 000000000..1eac19a1b
--- /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 000000000..9622cb374
--- /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 9ff7f3fa7..a6e62986d 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':