From b53e0717b7f6b042baaeec7f87999e99c76c031c Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Wed, 25 Jan 2023 13:57:32 -0600 Subject: [PATCH] Handle unmatched 404 when using prerender in dev mode (#5983) * fix(#5975): unmatched static paths should 404 during dev * chore: add changeset Co-authored-by: Nate Moore --- .changeset/fluffy-cherries-shake.md | 5 +++++ packages/astro/src/core/render/core.ts | 2 +- packages/astro/test/ssr-prerender-get-static-paths.test.js | 4 ++++ 3 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 .changeset/fluffy-cherries-shake.md diff --git a/.changeset/fluffy-cherries-shake.md b/.changeset/fluffy-cherries-shake.md new file mode 100644 index 000000000000..818ce7a42c59 --- /dev/null +++ b/.changeset/fluffy-cherries-shake.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes a dev server edge case where prerender + getStaticPaths would not 404 on an unmatched route diff --git a/packages/astro/src/core/render/core.ts b/packages/astro/src/core/render/core.ts index 6c486ae346b9..862ada7c8b9b 100644 --- a/packages/astro/src/core/render/core.ts +++ b/packages/astro/src/core/render/core.ts @@ -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 diff --git a/packages/astro/test/ssr-prerender-get-static-paths.test.js b/packages/astro/test/ssr-prerender-get-static-paths.test.js index 4ffa8677e539..b0253eabfc66 100644 --- a/packages/astro/test/ssr-prerender-get-static-paths.test.js +++ b/packages/astro/test/ssr-prerender-get-static-paths.test.js @@ -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 () => { @@ -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/); }); });