mirror of
https://github.com/withastro/astro.git
synced 2025-01-20 22:12:38 -05:00
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>
This commit is contained in:
parent
9001d063ae
commit
ec7d2ebbd9
7 changed files with 77 additions and 0 deletions
5
.changeset/sour-ties-sparkle.md
Normal file
5
.changeset/sour-ties-sparkle.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
"@astrojs/sitemap": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fixes URL generation for routes that rest parameters and start with `/`
|
|
@ -98,6 +98,8 @@ const createPlugin = (options?: SitemapOptions): AstroIntegration => {
|
||||||
.map((p) => {
|
.map((p) => {
|
||||||
if (p.pathname !== '' && !finalSiteUrl.pathname.endsWith('/'))
|
if (p.pathname !== '' && !finalSiteUrl.pathname.endsWith('/'))
|
||||||
finalSiteUrl.pathname += '/';
|
finalSiteUrl.pathname += '/';
|
||||||
|
if (p.pathname.startsWith('/'))
|
||||||
|
p.pathname = p.pathname.slice(1);
|
||||||
const fullPath = finalSiteUrl.pathname + p.pathname;
|
const fullPath = finalSiteUrl.pathname + p.pathname;
|
||||||
return new URL(fullPath, finalSiteUrl).href;
|
return new URL(fullPath, finalSiteUrl).href;
|
||||||
});
|
});
|
||||||
|
|
24
packages/integrations/sitemap/test/dynamic-path.test.js
Normal file
24
packages/integrations/sitemap/test/dynamic-path.test.js
Normal file
|
@ -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/'));
|
||||||
|
});
|
||||||
|
})
|
7
packages/integrations/sitemap/test/fixtures/dynamic/astro.config.mjs
vendored
Normal file
7
packages/integrations/sitemap/test/fixtures/dynamic/astro.config.mjs
vendored
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
import { defineConfig } from 'astro/config';
|
||||||
|
import sitemap from '@astrojs/sitemap';
|
||||||
|
|
||||||
|
export default defineConfig({
|
||||||
|
integrations: [sitemap()],
|
||||||
|
site: 'http://example.com'
|
||||||
|
})
|
9
packages/integrations/sitemap/test/fixtures/dynamic/package.json
vendored
Normal file
9
packages/integrations/sitemap/test/fixtures/dynamic/package.json
vendored
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
{
|
||||||
|
"name": "@test/sitemap-dynamic",
|
||||||
|
"version": "0.0.0",
|
||||||
|
"private": true,
|
||||||
|
"dependencies": {
|
||||||
|
"astro": "workspace:*",
|
||||||
|
"@astrojs/sitemap": "workspace:*"
|
||||||
|
}
|
||||||
|
}
|
21
packages/integrations/sitemap/test/fixtures/dynamic/src/pages/[...slug].astro
vendored
Normal file
21
packages/integrations/sitemap/test/fixtures/dynamic/src/pages/[...slug].astro
vendored
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
---
|
||||||
|
export async function getStaticPaths() {
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
params: {
|
||||||
|
slug: undefined,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
params: {
|
||||||
|
slug: '/blog'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
params: {
|
||||||
|
slug: '/test'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
];
|
||||||
|
}
|
||||||
|
---
|
9
pnpm-lock.yaml
generated
9
pnpm-lock.yaml
generated
|
@ -4577,6 +4577,15 @@ importers:
|
||||||
specifier: 0.6.2
|
specifier: 0.6.2
|
||||||
version: 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:
|
packages/integrations/sitemap/test/fixtures/ssr:
|
||||||
dependencies:
|
dependencies:
|
||||||
'@astrojs/sitemap':
|
'@astrojs/sitemap':
|
||||||
|
|
Loading…
Add table
Reference in a new issue