“Astro.response.headers must not be reassigned” appears when a page or component assigns a new value to Astro.response.headers, for example Astro.response.headers = new Headers({...}). Astro’s message explains: “Individual headers can be added to and removed from Astro.response.headers, but it must not be replaced with another instance of Headers altogether.” Keep the existing object and change it with set(), append() and delete().
Key Takeaways
Astro.response.headersis a getter with a setter that throws. Mutate it; never assign to it.- Use
set()to replace one header,append()to add another value,delete()to remove one.- The error hint mentions
add(), but standardHeadersobjects useset()andappend().- Headers only reach the browser on on-demand rendered pages; prerendered pages are plain files.
What triggers it
---
// src/pages/account.astro
export const prerender = false;
// Throws: Astro.response.headers must not be reassigned
Astro.response.headers = new Headers({
'Cache-Control': 'private, no-store',
});
---
Spreading into a new object fails the same way:
Astro.response.headers = { ...Astro.response.headers, 'X-Frame-Options': 'DENY' }; // also throws
Astro creates one Headers instance per request and builds the final Response from it. Replacing the instance would disconnect it from headers Astro and middleware already set, so the setter throws instead.
The fix: mutate the existing headers
---
// src/pages/account.astro
export const prerender = false;
Astro.response.headers.set('Cache-Control', 'private, no-store');
Astro.response.headers.set('X-Frame-Options', 'DENY');
Astro.response.headers.append('Vary', 'Cookie');
Astro.response.headers.delete('X-Powered-By');
---
<h1>Your account</h1>
To copy many headers from an object, loop over it:
const securityHeaders = {
'Referrer-Policy': 'strict-origin-when-cross-origin',
'X-Content-Type-Options': 'nosniff',
};
for (const [name, value] of Object.entries(securityHeaders)) {
Astro.response.headers.set(name, value);
}
The error hint suggests Astro.response.headers.add(). That method doesn’t exist on the web-standard Headers class, so use set() or append() as shown.
Setting headers in middleware
For headers that apply to every page, middleware is cleaner. There you work with the Response that next() returns:
// src/middleware.ts
import { defineMiddleware } from 'astro:middleware';
export const onRequest = defineMiddleware(async (context, next) => {
const response = await next();
response.headers.set('X-Content-Type-Options', 'nosniff');
return response;
});
If a response’s headers are immutable (some responses from fetch() are), create a new Response with copied headers and return that from middleware. The Astro middleware guide covers the full pattern.
Static pages and headers
On a prerendered page, Astro.response.headers doesn’t do anything at runtime; the page is a file on a CDN. Set headers for static sites in your host’s config, such as a _headers file on Cloudflare Pages or Netlify. Our static hosting docs show where each host reads them. Komal Astro themes are fully static, so their headers live in host config. See the official Astro.response reference for the other properties you can change.
FAQ
How do I set a header in an Astro page?
Call Astro.response.headers.set('Name', 'value') in the page frontmatter of an on-demand rendered page. Don’t assign a new object to Astro.response.headers.
Can I change the status code too?
Yes. Astro.response.status = 404 is allowed. Only the headers property is protected from reassignment.
Why don’t my headers show up in production?
The page is probably prerendered, so it’s served as a static file. Add export const prerender = false with an adapter, or set the headers in your host’s configuration.