Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check route collision after getStaticPaths #12028

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 } }));
}
Loading