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 leading slash creating incorrect conflict resolution between pages generated from static routes and rest parameters #10607

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
5 changes: 5 additions & 0 deletions .changeset/tender-apples-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Fixes an issue where a leading slash created incorrect conflict resolution between pages generated from static routes and rest parameters
3 changes: 2 additions & 1 deletion packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import type {
StylesheetAsset,
} from './types.js';
import { getTimeStat, shouldAppendForwardSlash } from './util.js';
import {stringifyParams} from "../routing/params.js";

function createEntryURL(filePath: string, outFolder: URL) {
return new URL('./' + filePath + `?time=${Date.now()}`, outFolder);
Expand Down Expand Up @@ -290,7 +291,7 @@ async function getPathsForRoute(
paths = staticPaths
.map((staticPath) => {
try {
return route.generate(staticPath.params);
return stringifyParams(staticPath.params, route);
} catch (e) {
if (e instanceof TypeError) {
throw getInvalidRouteSegmentError(e, route, staticPath);
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/routing/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ export function stringifyParams(params: GetStaticPathsItem['params'], route: Rou
return acc;
}, {} as Params);

return JSON.stringify(route.generate(validatedParams));
return route.generate(validatedParams);
}
12 changes: 12 additions & 0 deletions packages/astro/test/dynamic-route-collision.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ describe('Dynamic route collision', () => {
assert.equal($('h1').text(), 'Static About');
});

it('Builds a static nested index when in conflict with a dynamic route with slug with leading slash', async () => {
const html = await fixture.readFile('/test/index.html');
const $ = cheerio.load(html);
assert.equal($('h1').text(), 'Static Test');
});

it('Builds a static route when in conflict with a spread route', async () => {
const html = await fixture.readFile('/who/index.html');
const $ = cheerio.load(html);
Expand All @@ -33,6 +39,12 @@ describe('Dynamic route collision', () => {
assert.equal($('h1').text(), 'Static Tags Index');
});

it('Builds a static nested index when in conflict with a spread route with slug with leading slash', async () => {
const html = await fixture.readFile('/test/ing/index.html');
const $ = cheerio.load(html);
assert.equal($('h1').text(), 'Static TestIng');
});

it('Builds a static root index when in conflict with a spread route', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export async function getStaticPaths() {
slug: 'who',
title: 'Rest Who We Are',
},
{
slug: '/test/ing/',
title: 'Rest TestIng',
},
];
return pages.map(({ slug, title }) => {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export async function getStaticPaths() {
page: 'blog',
title: 'Dynamic Blog',
},
{
page: '/test',
title: 'Dynamic Test',
},
];
return pages.map(({ page, title }) => {
return {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
<title>Static Test</title>
</head>
<body>
<h1>Static Test</h1>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
<title>Static TestIng</title>
</head>
<body>
<h1>Static TestIng</h1>
</body>
</html>