Skip to content

Commit

Permalink
Handle unmatched 404 when using prerender in dev mode (#5983)
Browse files Browse the repository at this point in the history
* fix(#5975): unmatched static paths should 404 during dev

* chore: add changeset

Co-authored-by: Nate Moore <[email protected]>
  • Loading branch information
natemoo-re and natemoo-re authored Jan 25, 2023
1 parent 03e374f commit b53e071
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/fluffy-cherries-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes a dev server edge case where prerender + getStaticPaths would not 404 on an unmatched route
2 changes: 1 addition & 1 deletion packages/astro/src/core/render/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export async function getParamsAndProps(
routeCache.set(route, routeCacheEntry);
}
const matchedStaticPath = findPathItemByKey(routeCacheEntry.staticPaths, params, route);
if (!matchedStaticPath && !ssr) {
if (!matchedStaticPath && (ssr ? mod.prerender : true)) {
return GetParamsAndPropsError.NoMatchingStaticPath;
}
// Note: considered using Object.create(...) for performance
Expand Down
4 changes: 4 additions & 0 deletions packages/astro/test/ssr-prerender-get-static-paths.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ describe('prerender getStaticPaths - 404 behavior', () => {

it('resolves 404 on pattern match without static path - named params', async () => {
const res = await fixture.fetch('/pizza/provolone-pineapple');
const html = await res.text();
expect(res.status).to.equal(404);
expect(html).to.match(/404/);
});

it('resolves 200 on matching static path - rest params', async () => {
Expand All @@ -82,7 +84,9 @@ describe('prerender getStaticPaths - 404 behavior', () => {

it('resolves 404 on pattern match without static path - rest params', async () => {
const res = await fixture.fetch('/pizza/pizza-hut');
const html = await res.text();
expect(res.status).to.equal(404);
expect(html).to.match(/404/);
});
});

Expand Down

0 comments on commit b53e071

Please sign in to comment.