Skip to content

Commit

Permalink
Add info to SSR page on using Astro.response (withastro#8011)
Browse files Browse the repository at this point in the history
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
  • Loading branch information
2 people authored and wpplumber committed May 9, 2024
1 parent 1151b6f commit a3d1b7c
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion src/content/docs/en/guides/server-side-rendering.mdx
Expand Up @@ -222,7 +222,43 @@ See more details about [`Astro.cookies` and the `AstroCookie` type](/en/referenc

### `Response`

You can also return a [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) from any page using on-demand rendering.
[`Astro.response`](/en/reference/api-reference/#astroresponse) is a standard [`ResponseInit`](https://developer.mozilla.org/en-US/docs/Web/API/Response/Response#options) object. It can be used to set the response status and headers.

The example below sets a response status and status text for a product listing page when the product does not exist:

```astro title="src/pages/my-product.astro" {8-9}
---
import { getProduct } from '../api';
const product = await getProduct(Astro.params.id);
// No product found
if (!product) {
Astro.response.status = 404;
Astro.response.statusText = 'Not found';
}
---
<html>
<!-- Page here... -->
</html>
```

#### `Astro.response.headers`

You can set headers using the `Astro.response.headers` object:

```astro title="src/pages/index.astro" {2}
---
Astro.response.headers.set('Cache-Control', 'public, max-age=3600');
---
<html>
<!-- Page here... -->
</html>
```

#### Return a `Response` object

You can also return a [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) object directly from any page using on-demand rendering.

The example below returns a 404 on a dynamic page after looking up an id in the database:

Expand Down

0 comments on commit a3d1b7c

Please sign in to comment.