Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sitemap): url when rest parameter is used in page file names #9975

Merged
merged 4 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sour-ties-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/sitemap": patch
---

Fixes URL generation for routes that rest parameters and start with `/`
2 changes: 2 additions & 0 deletions packages/integrations/sitemap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down
24 changes: 24 additions & 0 deletions packages/integrations/sitemap/test/dynamic-path.test.js
Original file line number Diff line number Diff line change
@@ -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/'));
});
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'astro/config';
import sitemap from '@astrojs/sitemap';

export default defineConfig({
integrations: [sitemap()],
site: 'http://example.com'
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@test/sitemap-dynamic",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*",
"@astrojs/sitemap": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
export async function getStaticPaths() {
return [
{
params: {
slug: undefined,
}
},
{
params: {
slug: '/blog'
}
},
{
params: {
slug: '/test'
}
}
];
}
---
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading