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 routing behavior when getStaticPaths params include hyphens #7694

Merged
merged 2 commits into from
Jul 18, 2023
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/stupid-trains-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix route matching behavior when `getStaticPaths` result includes hyphenated params
4 changes: 2 additions & 2 deletions packages/astro/src/core/render/route-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export async function callGetStaticPaths({
keyedStaticPaths.keyed = new Map<string, GetStaticPathsItem>();

for (const sp of keyedStaticPaths) {
const paramsKey = stringifyParams(sp.params, route.component);
const paramsKey = stringifyParams(sp.params, route);
keyedStaticPaths.keyed.set(paramsKey, sp);
}

Expand Down Expand Up @@ -127,7 +127,7 @@ export function findPathItemByKey(
params: Params,
route: RouteData
) {
const paramsKey = stringifyParams(params, route.component);
const paramsKey = stringifyParams(params, route);
const matchedStaticPath = staticPaths.keyed.get(paramsKey);
if (matchedStaticPath) {
return matchedStaticPath;
Expand Down
9 changes: 4 additions & 5 deletions packages/astro/src/core/routing/params.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { GetStaticPathsItem, Params } from '../../@types/astro';
import type { GetStaticPathsItem, RouteData, Params } from '../../@types/astro';
import { validateGetStaticPathsParameter } from './validation.js';

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

// Always sort keys before stringifying to make sure objects match regardless of parameter ordering
return JSON.stringify(validatedParams, Object.keys(params).sort());
return JSON.stringify(route.generate(validatedParams))
}
5 changes: 5 additions & 0 deletions packages/astro/test/astro-get-static-paths.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,9 @@ describe('getStaticPaths - dev calls', () => {
);
}
});

it('properly handles hyphenation in getStaticPaths', async () => {
const res = await fixture.fetch('/pizza/parmesan-and-olives');
expect(res.status).to.equal(200);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ export function getStaticPaths() {
params: { cheese: 'mozzarella', topping: 'pepperoni' },
}, {
params: { cheese: 'provolone', topping: 'sausage' },
}, {
// fix(#7265): hyphenated behavior
params: { cheese: 'parmesan-and', topping: 'olives' },
}]
}
const { cheese, topping } = Astro.params
Expand Down
Loading