Fix getStaticPaths regressions with nested arrays (#5375)

* Fix getStaticPaths regression

* Add changeset

* Add test
This commit is contained in:
Erika 2022-11-11 17:50:40 -04:00 committed by GitHub
parent 591153f457
commit f9104354b8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fix regression causing nested arrays in `getStaticPaths`'s return value to throw an error

View file

@ -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>();

View file

@ -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');

View file

@ -0,0 +1,8 @@
---
export function getStaticPaths() {
return [
[ { params: {slug: "slug1"} } ],
[ { params: {slug: "slug2"} } ],
]
}
---