Skip to content

Commit

Permalink
fix(cloudflare): routing strategy doesn't support on-demand 404 (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderniebuhr authored Nov 16, 2023
1 parent bbd30c0 commit 473e9fa
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/spotty-kings-roll.md
Original file line number Diff line number Diff line change
@@ -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.
11 changes: 8 additions & 3 deletions packages/cloudflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit 473e9fa

Please sign in to comment.