From 06c255716ae8e922fb9d4ffa5595cbb34146fff6 Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Tue, 18 Jul 2023 04:36:43 -0500 Subject: [PATCH] Fix routing behavior when getStaticPaths params include hyphens (#7694) --- .changeset/stupid-trains-move.md | 5 +++++ packages/astro/src/core/render/route-cache.ts | 4 ++-- packages/astro/src/core/routing/params.ts | 9 ++++----- packages/astro/test/astro-get-static-paths.test.js | 5 +++++ .../src/pages/pizza/[cheese]-[topping].astro | 3 +++ 5 files changed, 19 insertions(+), 7 deletions(-) create mode 100644 .changeset/stupid-trains-move.md diff --git a/.changeset/stupid-trains-move.md b/.changeset/stupid-trains-move.md new file mode 100644 index 000000000000..ceab16a585b4 --- /dev/null +++ b/.changeset/stupid-trains-move.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix route matching behavior when `getStaticPaths` result includes hyphenated params diff --git a/packages/astro/src/core/render/route-cache.ts b/packages/astro/src/core/render/route-cache.ts index 787b345518e4..71b2966dea35 100644 --- a/packages/astro/src/core/render/route-cache.ts +++ b/packages/astro/src/core/render/route-cache.ts @@ -71,7 +71,7 @@ export async function callGetStaticPaths({ keyedStaticPaths.keyed = new Map(); for (const sp of keyedStaticPaths) { - const paramsKey = stringifyParams(sp.params, route.component); + const paramsKey = stringifyParams(sp.params, route); keyedStaticPaths.keyed.set(paramsKey, sp); } @@ -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; diff --git a/packages/astro/src/core/routing/params.ts b/packages/astro/src/core/routing/params.ts index 6a822861fecf..4177db6b6185 100644 --- a/packages/astro/src/core/routing/params.ts +++ b/packages/astro/src/core/routing/params.ts @@ -1,4 +1,4 @@ -import type { GetStaticPathsItem, Params } from '../../@types/astro'; +import type { GetStaticPathsItem, RouteData, Params } from '../../@types/astro'; import { validateGetStaticPathsParameter } from './validation.js'; /** @@ -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)) } diff --git a/packages/astro/test/astro-get-static-paths.test.js b/packages/astro/test/astro-get-static-paths.test.js index 9ff97831c591..784ff17186b9 100644 --- a/packages/astro/test/astro-get-static-paths.test.js +++ b/packages/astro/test/astro-get-static-paths.test.js @@ -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); + }); }); diff --git a/packages/astro/test/fixtures/astro-get-static-paths/src/pages/pizza/[cheese]-[topping].astro b/packages/astro/test/fixtures/astro-get-static-paths/src/pages/pizza/[cheese]-[topping].astro index a698a76d784a..162ead99e109 100644 --- a/packages/astro/test/fixtures/astro-get-static-paths/src/pages/pizza/[cheese]-[topping].astro +++ b/packages/astro/test/fixtures/astro-get-static-paths/src/pages/pizza/[cheese]-[topping].astro @@ -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