Skip to content

Commit 06c2557

Browse files
authored
Fix routing behavior when getStaticPaths params include hyphens (#7694)
1 parent d80e5fc commit 06c2557

5 files changed

Lines changed: 19 additions & 7 deletions

File tree

.changeset/stupid-trains-move.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fix route matching behavior when `getStaticPaths` result includes hyphenated params

packages/astro/src/core/render/route-cache.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export async function callGetStaticPaths({
7171
keyedStaticPaths.keyed = new Map<string, GetStaticPathsItem>();
7272

7373
for (const sp of keyedStaticPaths) {
74-
const paramsKey = stringifyParams(sp.params, route.component);
74+
const paramsKey = stringifyParams(sp.params, route);
7575
keyedStaticPaths.keyed.set(paramsKey, sp);
7676
}
7777

@@ -127,7 +127,7 @@ export function findPathItemByKey(
127127
params: Params,
128128
route: RouteData
129129
) {
130-
const paramsKey = stringifyParams(params, route.component);
130+
const paramsKey = stringifyParams(params, route);
131131
const matchedStaticPath = staticPaths.keyed.get(paramsKey);
132132
if (matchedStaticPath) {
133133
return matchedStaticPath;

packages/astro/src/core/routing/params.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { GetStaticPathsItem, Params } from '../../@types/astro';
1+
import type { GetStaticPathsItem, RouteData, Params } from '../../@types/astro';
22
import { validateGetStaticPathsParameter } from './validation.js';
33

44
/**
@@ -27,15 +27,14 @@ export function getParams(array: string[]) {
2727
* values and create a stringified key for the route
2828
* that can be used to match request routes
2929
*/
30-
export function stringifyParams(params: GetStaticPathsItem['params'], routeComponent: string) {
30+
export function stringifyParams(params: GetStaticPathsItem['params'], route: RouteData) {
3131
// validate parameter values then stringify each value
3232
const validatedParams = Object.entries(params).reduce((acc, next) => {
33-
validateGetStaticPathsParameter(next, routeComponent);
33+
validateGetStaticPathsParameter(next, route.component);
3434
const [key, value] = next;
3535
acc[key] = value?.toString();
3636
return acc;
3737
}, {} as Params);
3838

39-
// Always sort keys before stringifying to make sure objects match regardless of parameter ordering
40-
return JSON.stringify(validatedParams, Object.keys(params).sort());
39+
return JSON.stringify(route.generate(validatedParams))
4140
}

packages/astro/test/astro-get-static-paths.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,9 @@ describe('getStaticPaths - dev calls', () => {
126126
);
127127
}
128128
});
129+
130+
it('properly handles hyphenation in getStaticPaths', async () => {
131+
const res = await fixture.fetch('/pizza/parmesan-and-olives');
132+
expect(res.status).to.equal(200);
133+
});
129134
});

packages/astro/test/fixtures/astro-get-static-paths/src/pages/pizza/[cheese]-[topping].astro

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ export function getStaticPaths() {
44
params: { cheese: 'mozzarella', topping: 'pepperoni' },
55
}, {
66
params: { cheese: 'provolone', topping: 'sausage' },
7+
}, {
8+
// fix(#7265): hyphenated behavior
9+
params: { cheese: 'parmesan-and', topping: 'olives' },
710
}]
811
}
912
const { cheese, topping } = Astro.params

0 commit comments

Comments
 (0)