withastro / astro

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

[WARN] `Astro.request.headers` is not available in "static" output mode. #10212

Closed mrcnski closed 8 months ago

mrcnski commented 8 months ago

Astro Info

Astro                    v4.4.3
Node                     v21.5.0
System                   macOS (arm64)
Package Manager          npm
Output                   static
Adapter                  none
Integrations             none

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

No response

Describe the Bug

Since upgrading to Astro 4.4.3 I'm getting this warning in the console:

[WARN] `Astro.request.headers` is not available in "static" output mode. To enable header access: set `output: "server"` or `output: "hybrid"` in your config file.

What's the expected result?

I was not getting this warning on Astro 4.3.7.

I'm not doing anything with headers AFAIK, but it would be nice to have a trace to know where this warning is coming from.

I tried for some time but I'm having trouble reproducing this on Stackblitz or in a fresh project. As mentioned above, a trace would be nice. My project is simple, my only dependencies are Astro and Typescript.

Link to Minimal Reproducible Example

https://stackblitz.com/see-above

Participation

Reyno-Skeps commented 8 months ago

Can confirm, running into the same issue right now.

Princesseuh commented 8 months ago

Will be fixed by https://github.com/withastro/astro/pull/10196, sorry for the inconvenience. It's a bug with i18n in particular

mrcnski commented 8 months ago

Awesome! Sorry, I searched existing issues but didn't think to search PRs.

bluwy commented 8 months ago

https://github.com/withastro/astro/pull/10196 has been merged

mrcnski commented 8 months ago

I've confirmed that this has been fixed as of 4.4.5, thanks everybody.

eitelkrauss commented 7 months ago

I just upgraded to 4.5.9 and I'm running into this issue. The warning is only for my prerendered pages. I have output: 'server'

boredland commented 7 months ago

can confirm this still is a problem with hybrid, i18n and such a .astro file:

---
import { getHeadersLocale } from '../utils/headersLocale'

const locale = getHeadersLocale(Astro.request.headers.get('accept-language') ?? 'en')

return Astro.redirect(`/${locale}`)
---
matthewp commented 7 months ago

This is intentional. You can't read headers in a static page. We do this on purpose so that you don't rely on behavior that will lead to build-time and run-time differences.

eitelkrauss commented 7 months ago

@matthewp I'm not reading any headers though.

EDIT: I was reading headers.. sorry

daolanfler commented 7 months ago
---
import ThemeToggle from "./ThemeToggle";
const cookie = Astro.cookies.get("theme");

export const prerender = false;
---

<ThemeToggle themeCookie={cookie?.value} client:load />

I have a ThemeToggle component like this, this component is accessing cookie in headers. And my astro.config.mjs is output: hybrid. This component runs on server instead of client as I've specified prerender=false, but I still get the warning, and I thinks it's ok to access headers in this situation...

rottmann commented 6 months ago

Hint: this error occurs on symbolic linked files too, even if prerender is set to false.

xabru commented 1 month ago

I encountered the same warning again in the latest update "4.16.1." I had to downgrade to "4.16.0" to make the warning disappear in a project created from scratch using bun create astro.

jahands commented 1 month ago

Same here, running Astro on Cloudflare Workers. Just upgraded and started getting this warning. I also have an endpoint that started failing with this error on the server side:

(error) 12:28:48 [ERROR] TypeError: Can't modify immutable headers.
    at setOriginHeader (_worker.js:163:25960)
    at RenderContext.create (_worker.js:165:3228)
    at async bn.render (_worker.js:180:186)
    at async Object.fetch (_worker.js:180:5208)

(not sure if related or if this is a separate issue.) edit: separate issue: https://github.com/withastro/astro/issues/12201