Astro.response

Astro.response lets a page set the status code and headers of the outgoing response.

Since Astro 1.0 Spec ↗

Syntax

Astro.response.status = 404

Returns

ResponseInit — A mutable object with status, statusText, and a headers map.

Examples

---
const post = await getEntry('blog', Astro.params.slug);
if (!post) {
  Astro.response.status = 404;
}
Astro.response.headers.set('Cache-Control', 'public, max-age=3600');
---
{post ? <article>...</article> : <p>Not found</p>}
Output
Serves a 404 status for missing posts and adds a cache header otherwise.

Notes

Header and status changes apply only in on-demand (SSR) rendered routes. Mutate it from the frontmatter before output. For full control of the body, return a Response directly instead.

See also