From 473e9fabdc2f5a87daf6a71c8869e8430903590f Mon Sep 17 00:00:00 2001 From: Alexander Niebuhr Date: Thu, 16 Nov 2023 17:09:21 +0100 Subject: [PATCH] fix(cloudflare): routing strategy doesn't support on-demand 404 (#69) --- .changeset/spotty-kings-roll.md | 5 +++++ packages/cloudflare/src/index.ts | 11 ++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) create mode 100644 .changeset/spotty-kings-roll.md diff --git a/.changeset/spotty-kings-roll.md b/.changeset/spotty-kings-roll.md new file mode 100644 index 00000000..4bf65d46 --- /dev/null +++ b/.changeset/spotty-kings-roll.md @@ -0,0 +1,5 @@ +--- +'@astrojs/cloudflare': patch +--- + +Fixes a regression which caused the adapter to falsely generate `_routes.json` for on-demand rendered 404 pages, which causes unexpected behavior in Cloudflare's SPA routing. diff --git a/packages/cloudflare/src/index.ts b/packages/cloudflare/src/index.ts index 117854ef..af078226 100644 --- a/packages/cloudflare/src/index.ts +++ b/packages/cloudflare/src/index.ts @@ -490,12 +490,14 @@ export default function createIntegration(args?: Options): AstroIntegration { /** * These route types are candiates for being part of the `_routes.json` `include` array. */ + let notFoundIsSSR = false; const potentialFunctionRouteTypes = ['endpoint', 'page']; - const functionEndpoints = routes // Certain route types, when their prerender option is set to false, run on the server as function invocations .filter((route) => potentialFunctionRouteTypes.includes(route.type) && !route.prerender) .map((route) => { + if (route.component === 'src/pages/404.astro' && route.prerender === false) + notFoundIsSSR = true; const includePattern = '/' + route.segments @@ -637,8 +639,11 @@ export default function createIntegration(args?: Options): AstroIntegration { ? excludeStrategy.include.length + excludeStrategy.exclude.length : Infinity; - const winningStrategy = - includeStrategyLength <= excludeStrategyLength ? includeStrategy : excludeStrategy; + const winningStrategy = notFoundIsSSR + ? excludeStrategy + : includeStrategyLength <= excludeStrategyLength + ? includeStrategy + : excludeStrategy; await fs.promises.writeFile( new URL('./_routes.json', _config.outDir),