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:
parent
40e7a1b05d
commit
d3bd673392
4 changed files with 29 additions and 6 deletions
5
.changeset/light-pianos-sip.md
Normal file
5
.changeset/light-pianos-sip.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Handles route collision detection only if it matches `getStaticPaths`
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
14
packages/astro/test/fixtures/dynamic-endpoint-collision/src/pages/api/safe/[...slug].ts
vendored
Normal file
14
packages/astro/test/fixtures/dynamic-endpoint-collision/src/pages/api/safe/[...slug].ts
vendored
Normal 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 } }));
|
||||
}
|
Loading…
Add table
Reference in a new issue