withastro / astro

The web framework for content-driven websites. ⭐️ Star to support our work!
https://astro.build
Other
46.08k stars 2.43k forks source link

TypeError: Can't modify immutable headers. in Cloudflare Pages after updating to 4.16.1 #12201

Open nicbovee opened 3 hours ago

nicbovee commented 3 hours ago

Astro Info

Astro                    v4.16.1
Node                     v20.16.0
System                   macOS (arm64)
Package Manager          pnpm
Output                   hybrid
Adapter                  @astrojs/cloudflare
Integrations             @astrojs/react
                         @astrojs/svelte
                         @astrojs/tailwind
                         @sanity/astro
                         @sentry/astro

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

No response

Describe the Bug

After building with 4.16.1 my site running on Cloudflare Pages in hybrid mode 500's on all non-prerendered routes.

Looking at the realtime logs I see complaints about headers:

18:17:35 [ERROR] TypeError: Can't modify immutable headers.\n    at setOriginHeader (chunks/index_CUH8Sv5d.mjs:1356:19)\n    at RenderContext.create (chunks/index_CUH8Sv5d.mjs:1393:5)\n    at async App.render (chunks/_@astrojs-ssr-adapter_BtNH5sZC.mjs:824:29)\n    at async Object.fetch (chunks/_@astrojs-ssr-adapter_BtNH5sZC.mjs:1016:22)

In the build output there's also a bunch of warnings now that hadn't been there up to this point:

11:28:16.727    18:28:16   ├─ /daily-readings/it-must-be-small/index.html18:28:16 [WARN] `Astro.request.headers` is unavailable in "static" output mode, and in prerendered pages within "hybrid" and "server" output modes. If you need access to request headers, make sure that `output` is configured as either `"server"` or `output: "hybrid"` in your config file, and that the page accessing the headers is rendered on-demand.
11:28:16.731     (+4ms)

Locking the version to 4.16.0 and re-deploying fixed it.

What's the expected result?

I would expect these routes to work.

Link to Minimal Reproducible Example

https://hotfix-hide-entire-countdown.wildatheart1.pages.dev/the-series

Participation

nicbovee commented 3 hours ago

Possibly caused by this change: https://github.com/withastro/astro/commit/2d10de5f212323e6e19c7ea379826dcc18fe739c#diff-153aea9d29dc6f65b7f594f4674159c48939a8402408a2e11742275c19c3dc94R86

nicbovee commented 3 hours ago

Wondering if this should change to:

export function setOriginHeader(request: Request, pathname: string): void {
    request.headers.append(ASTRO_ORIGIN_HEADER, encodeURIComponent(pathname));
}

I'm missing context around this, but it looks like X-Astro-Origin is a new header when setOriginHeader is called, which makes me think .append is the right method to use based on these docs from cloudflare.

flashblaze commented 1 hour ago

For some reason, if I build locally and then deploy using wrangler pages deploy dist this does not happen for me. However, if I push the code to GitHub and Cloudflare builds it automatically, I get the same error.

I'm on astro version 4.16.0