summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Erika <3019731+Princesseuh@users.noreply.github.com> 2022-11-11 17:50:40 -0400
committerGravatar GitHub <noreply@github.com> 2022-11-11 17:50:40 -0400
commitf9104354b8a2c2457b9cd64405ddf8a832628e26 (patch)
tree241fba958eb657ca4bb6c8068b8a45c785a52c5d
parent591153f457664bd3f654d0c9a4362b85e4061c4d (diff)
downloadastro-f9104354b8a2c2457b9cd64405ddf8a832628e26.tar.gz
astro-f9104354b8a2c2457b9cd64405ddf8a832628e26.tar.zst
astro-f9104354b8a2c2457b9cd64405ddf8a832628e26.zip
Fix `getStaticPaths` regressions with nested arrays (#5375)
* Fix getStaticPaths regression * Add changeset * Add test
-rw-r--r--.changeset/angry-dragons-drive.md5
-rw-r--r--packages/astro/src/core/render/route-cache.ts6
-rw-r--r--packages/astro/test/astro-get-static-paths.test.js5
-rw-r--r--packages/astro/test/fixtures/astro-get-static-paths/src/pages/nested-arrays/[slug].astro8
4 files changed, 23 insertions, 1 deletions
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<string, GetStaticPathsItem>();
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"} } ],
+ ]
+ }
+---