From f9104354b8a2c2457b9cd64405ddf8a832628e26 Mon Sep 17 00:00:00 2001 From: Erika <3019731+Princesseuh@users.noreply.github.com> Date: Fri, 11 Nov 2022 17:50:40 -0400 Subject: [PATCH] Fix `getStaticPaths` regressions with nested arrays (#5375) * Fix getStaticPaths regression * Add changeset * Add test --- .changeset/angry-dragons-drive.md | 5 +++++ packages/astro/src/core/render/route-cache.ts | 6 +++++- packages/astro/test/astro-get-static-paths.test.js | 5 +++++ .../src/pages/nested-arrays/[slug].astro | 8 ++++++++ 4 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 .changeset/angry-dragons-drive.md create mode 100644 packages/astro/test/fixtures/astro-get-static-paths/src/pages/nested-arrays/[slug].astro diff --git a/.changeset/angry-dragons-drive.md b/.changeset/angry-dragons-drive.md new file mode 100644 index 000000000..a33dba38f --- /dev/null +++ b/.changeset/angry-dragons-drive.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix regression causing nested arrays in `getStaticPaths`'s return value to throw an error diff --git a/packages/astro/src/core/render/route-cache.ts b/packages/astro/src/core/render/route-cache.ts index 26cf4f6fe..35cbaab3e 100644 --- a/packages/astro/src/core/render/route-cache.ts +++ b/packages/astro/src/core/render/route-cache.ts @@ -49,11 +49,15 @@ export async function callGetStaticPaths({ }, }); + // Flatten the array before validating the content, otherwise users using `.map` will run into errors + if (Array.isArray(staticPaths)) { + staticPaths = staticPaths.flat(); + } + if (isValidate) { validateGetStaticPathsResult(staticPaths, logging, route); } - staticPaths = staticPaths.flat(); const keyedStaticPaths = staticPaths as GetStaticPathsResultKeyed; keyedStaticPaths.keyed = new Map(); diff --git a/packages/astro/test/astro-get-static-paths.test.js b/packages/astro/test/astro-get-static-paths.test.js index 80b81140a..3211f3318 100644 --- a/packages/astro/test/astro-get-static-paths.test.js +++ b/packages/astro/test/astro-get-static-paths.test.js @@ -101,6 +101,11 @@ describe('getStaticPaths - route params type validation', () => { await devServer.stop(); }); + it('resolves 200 on nested array parameters', async () => { + const res = await fixture.fetch('/nested-arrays/slug1'); + expect(res.status).to.equal(200); + }); + it('resolves 200 on matching static path - string params', async () => { // route provided with { params: { year: "2022", slug: "post-2" }} const res = await fixture.fetch('/blog/2022/post-1'); diff --git a/packages/astro/test/fixtures/astro-get-static-paths/src/pages/nested-arrays/[slug].astro b/packages/astro/test/fixtures/astro-get-static-paths/src/pages/nested-arrays/[slug].astro new file mode 100644 index 000000000..9bd7b4f41 --- /dev/null +++ b/packages/astro/test/fixtures/astro-get-static-paths/src/pages/nested-arrays/[slug].astro @@ -0,0 +1,8 @@ +--- + export function getStaticPaths() { + return [ + [ { params: {slug: "slug1"} } ], + [ { params: {slug: "slug2"} } ], + ] + } +---