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

[@astrojs/cloudflare] Cloudflare Pages Adapter does not Render SSR 404 Page #504

Open
1 task done
Kynson opened this issue Jan 15, 2025 · 2 comments
Open
1 task done

Comments

@Kynson
Copy link

Kynson commented Jan 15, 2025

Astro Info

Astro                    v5.1.6
Node                     v20.9.0
System                   Linux (x64)
Package Manager          npm
Output                   static
Adapter                  @astrojs/cloudflare
Integrations             none

Describe the Bug

Situation

Setting const prerender = false on the 404 page; other pages are statically generated.

Reproducing:

  1. Build and start the preview using wrangler pages dev ./dist
  2. Navigate to a non existing page.

Result

The 404 page is not rendered, instead, the index page is rendered.

Possible cause

const { pathname } = new URL(request.url);
if (manifest.assets.has(pathname)) {
  return env.ASSETS.fetch(request.url.replace(/\.html$/, ''));
}
const routeData = app.match(request);
if (!routeData) {
  const asset = await env.ASSETS.fetch(
    request.url.replace(/index.html$/, '').replace(/\.html$/, ''),
  );
  if (asset.status !== 404) {
    return asset;
  }
}

The above is extracted from the generated code (_workers.js/chunks/_@astrojs-ssr-adapter_<hash>.mjs). As the 404 page is server-side rendered, there is no 404.html generated. Hence, Cloudflare Pages assumes the app is a SPA and rewriting all paths to index.html. This leads to an issue that asset.status is 200 instead of 400 and the asset is index.html.

Workaround

  1. Don't use SSR for 404
  2. Add a fake 404.html to dist

What's the expected result?

The 404 page should be rendered.

Link to Minimal Reproducible Example

https://codesandbox.io/p/devbox/sfgjcf

Participation

  • I am willing to submit a pull request for this issue.
@Kynson
Copy link
Author

Kynson commented Jan 16, 2025

Personally, I think one way to solve this is for the adapter to generate an empty 404.html (possibly configurable by an option in astro.config.mjs? e.g. generate404/ isSPA etc). env.ASSETS.fetch will return 404 when it sees a 404.html and Astro will render the 404.astro.

@alexanderniebuhr
Copy link
Member

What you observe is the expected behavior. If Cloudflare doesn't see an static 404.html, it expects SPA mode, and behaves like your described. So far so good. Now the question is how to handle this. One way is to tell users to set prerender = true, the other is to inject an empty page, which could also be a bit unexpected for some. Not sure what the best solution is here. I'll ask the team in Discord :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants