withastro / withastro/docs

Seems like Layouts CAN set HTTP response headers, contrary to the docs?

Open
#11,843 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

improve or update documentation
Dominant language
MDX
Stars
1.7k
Forks
1.9k
Avg merge
1d 12h
Merged PRs (30d)
97

Description

📚 Subject area/topic

On-demand rendering, setting response headers in layouts

📋 Page(s) affected (or suggested, for new content)

https://docs.astro.build/en/guides/on-demand-rendering/#on-demand-rendering-features

📋 Description of content that is out-of-date or incorrect

On the on-demand rendering page, there is a big warning that HTTP headers cannot be modified inside layouts, only pages:

Image

However, as far as I can tell, this isn't true?

Starting from npm create astro@latest and using the basic template:

/src/pages/index.astro:

---
import Welcome from '../components/Welcome.astro';
import Layout from '../layouts/Layout.astro';

Astro.response.headers.append('header-sources', '/pages/index.astro')
---

<Layout>
	<Welcome />
</Layout>

/src/layouts/Layout.astro:

---
Astro.response.headers.append('header-sources', '/layouts/Layout.astro')
---
<!doctype html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<meta name="viewport" content="width=device-width" />
		<link rel="icon" type="image/svg+xml" href="/favicon.svg" />

[...snipped...]

It seems like the layout DOES affect the final headers:

Image

I'm not sure if this is a documentation error, or if there's some other way of setting headers that isn't supported, but Astro.response.headers() does work...? Or are there some edge cases in which it won't...?

🖥️ Reproduction in StackBlitz (if reporting incorrect content or code samples)

https://stackblitz.com/~/github.com/arcataroger/astro-header-modification-in-layout

Inside StackBlitz, I think you have to open the browser inspector and then reload the preview iframe:

Image

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read the on-demand-rendering page section linked in the issue and reproduce the behavior with the provided StackBlitz example. Verify whether headers set in src/layouts/Layout.astro are consistently included in the final response, including any documented edge cases. Done means the warning is corrected or clarified based on the verified behavior.

Written by the indexing model from the issue text.

Assessment

Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.