0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-03-17 23:11:29 -05:00

Check route collision after getStaticPaths (#12028)

This commit is contained in:
Bjorn Lu 2024-09-19 20:43:01 +08:00 committed by GitHub
parent 40e7a1b05d
commit d3bd673392
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 29 additions and 6 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Handles route collision detection only if it matches `getStaticPaths`

View file

@ -33,12 +33,6 @@ export async function getProps(opts: GetParamsAndPropsOptions): Promise<Props> {
return {};
}
// This is a dynamic route, start getting the params
const params = getParams(route, pathname);
if (mod) {
validatePrerenderEndpointCollision(route, mod, params);
}
// During build, the route cache should already be populated.
// During development, the route cache is filled on-demand and may be empty.
const staticPaths = await callGetStaticPaths({
@ -49,6 +43,7 @@ export async function getProps(opts: GetParamsAndPropsOptions): Promise<Props> {
ssr: serverLike,
});
const params = getParams(route, pathname);
const matchedStaticPath = findPathItemByKey(staticPaths, params, route, logger);
if (!matchedStaticPath && (serverLike ? route.prerender : true)) {
throw new AstroError({
@ -58,6 +53,10 @@ export async function getProps(opts: GetParamsAndPropsOptions): Promise<Props> {
});
}
if (mod) {
validatePrerenderEndpointCollision(route, mod, params);
}
const props: Props = matchedStaticPath?.props ? { ...matchedStaticPath.props } : {};
return props;

View file

@ -49,5 +49,10 @@ describe('Dynamic endpoint collision', () => {
const res = await fixture.fetch('/api/catch/one').then((r) => r.text());
assert.equal(res, '{"slug":"one"}');
});
it('returns 404 when user visits dynamic endpoint that has collision but not specified in getStaticPaths', async () => {
const res = await fixture.fetch('/api/safe');
assert.equal(res.status, 404);
});
});
});

View file

@ -0,0 +1,14 @@
import type { APIRoute } from 'astro';
// No undefined so should not error
const slugs = ['one'];
export const GET: APIRoute = ({ params }) => {
return Response.json({
slug: params.slug || 'index',
});
};
export function getStaticPaths() {
return slugs.map((u) => ({ params: { slug: u } }));
}