From 97c8760d78ffd172149f7776442725861576fba7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Alves?= <71379045+andremralves@users.noreply.github.com> Date: Fri, 18 Aug 2023 12:27:44 -0300 Subject: [PATCH] Fix 404 response leading to an infinite loop when there is no 404 page (#8136) * fix: 404 response leads to infinite loop * chore: changeset --------- Co-authored-by: Nate Moore --- .changeset/lemon-comics-jump.md | 5 +++++ packages/astro/src/vite-plugin-astro-server/route.ts | 6 +++++- 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 .changeset/lemon-comics-jump.md diff --git a/.changeset/lemon-comics-jump.md b/.changeset/lemon-comics-jump.md new file mode 100644 index 000000000..f2c9255f3 --- /dev/null +++ b/.changeset/lemon-comics-jump.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix 404 response leading to an infinite loop when there is no 404 page. diff --git a/packages/astro/src/vite-plugin-astro-server/route.ts b/packages/astro/src/vite-plugin-astro-server/route.ts index f58d248a3..f387d07a4 100644 --- a/packages/astro/src/vite-plugin-astro-server/route.ts +++ b/packages/astro/src/vite-plugin-astro-server/route.ts @@ -259,7 +259,7 @@ export async function handleRoute({ await writeWebResponse(incomingResponse, response); } } else { - if (result.status === 404) { + if (result.status === 404 && has404Route(manifestData)) { const fourOhFourRoute = await matchRoute('/404', env, manifestData); return handleRoute({ ...options, @@ -380,3 +380,7 @@ function getStatus(matchedRoute?: MatchedRoute): 404 | 500 | undefined { if (matchedRoute.route.route === '/404') return 404; if (matchedRoute.route.route === '/500') return 500; } + +function has404Route(manifest: ManifestData): RouteData | undefined { + return manifest.routes.find((route) => route.route === '/404') +}