Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cloudflare): routing strategy doesn't support on-demand 404 #69

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
alexanderniebuhr marked this conversation as resolved.
Show resolved Hide resolved
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