Skip to content

Commit

Permalink
fix(#7265): update keyed static paths implementation to improve hyphe…
Browse files Browse the repository at this point in the history
…nated usage
  • Loading branch information
natemoo-re committed Jul 17, 2023
1 parent cc0f81c commit fa668a0
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
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

0 comments on commit fa668a0

Please sign in to comment.