Skip to content

Prerendered 404 page doesn't work on Cloudflare pages: error code: 1042 #13932

Description

@millcrest-cto

Astro Info

Astro                    v5.8.1
Node                     v22.15.0
System                   Linux (x64)
Package Manager          npm
Output                   server
Adapter                  @astrojs/cloudflare
Integrations             @sentry/astro
                         @astrojs/sitemap

Clipboard command not found!
Please manually copy the text above.

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

Bug: 404 Page Returns Error 1042/522 on Cloudflare Pages with Server Mode

Description

Using output: "server" with prerendered 404.astro, non-existent routes return error 1042 (workers.dev) or 522 (proxy) instead of serving the custom 404 page on Cloudflare Pages.

Environment

  • Astro 5.x with @astrojs/cloudflare adapter
  • Cloudflare Pages deployment
  • Server mode with selective prerendering

Reproduction

  1. Set output: "server" in astro.config.mjs
  2. Create 404.astro with export const prerender = true
  3. Deploy to Cloudflare Pages
  4. Visit non-existent route → 522 timeout instead of 404 page

What Works/Doesn't Work

✅ Direct /404 access works
✅ All prerendered pages work
✅ Dev server works perfectly
❌ Any non-existent route fails

Root Cause

Cloudflare Pages routes unknown URLs to Astro server function instead of falling back to static 404.html. Error 1042 suggests the server function may be trying to make internal fetch requests when handling 404s, which Cloudflare Workers prohibits for security reasons ("You can't fetch between workers with the same account").

Attempted Solutions

  • Added _routes.json with explicit routing rules
  • Created standalone 404.astro without dependencies
  • Verified correct build output (dist/404.html exists)
  • Tested various Cloudflare Pages configurations
  • Cache clearing and fresh deployments
  • Removed all Astro integrations other than Cloudflare adaptor
  • Tried setting output to "static"

All attempts failed - issue persists.

What's the expected result?

Expected vs Actual

  • Expected: Serves generated dist/404.html with 404 status
  • Actual:
    • Error 1042 on *.workers.dev URLs
    • Error 522 (timeout) when behind Cloudflare proxy
    • No function logs in Cloudflare dashboard

Link to Minimal Reproducible Example

https://github.com/millcrest-cto/minimal-404-test

Participation

  • I am willing to submit a pull request for this issue.

Metadata

Metadata

Labels

- P2: has workaroundAn edge case that only affects very specific usage, but has a trivial workaround (priority)pkg: cloudflareRelated to the Cloudflare adapter

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions