Skip to content

Commit

Permalink
Check route collision after getStaticPaths (#12028)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Sep 19, 2024
1 parent 40e7a1b commit d3bd673
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/light-pianos-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Handles route collision detection only if it matches `getStaticPaths`
11 changes: 5 additions & 6 deletions packages/astro/src/core/render/params-and-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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({
Expand All @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions packages/astro/test/dynamic-endpoint-collision.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
Original file line number Diff line number Diff line change
@@ -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 } }));
}

0 comments on commit d3bd673

Please sign in to comment.