Skip to content

Commit

Permalink
update a code snippet in guides/server-side-rendering.mdx (#9700)
Browse files Browse the repository at this point in the history
Co-authored-by: Sarah Rainsberger <[email protected]>
  • Loading branch information
ArmandPhilippot and sarah11918 authored Oct 16, 2024
1 parent 65e9b44 commit ce6715b
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/content/docs/en/guides/server-side-rendering.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,10 @@ See more details about [`Astro.cookies` and the `AstroCookie` type](/en/referenc

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}
```astro title="src/pages/product/[id].astro" {10,11}
---
export const prerender = false; // Not needed in 'server' mode
import { getProduct } from '../api';
const product = await getProduct(Astro.params.id);
Expand Down Expand Up @@ -262,12 +264,14 @@ Astro.response.headers.set('Cache-Control', 'public, max-age=3600');

#### 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.
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 either manually or with [`Astro.redirect`](/en/reference/api-reference/#astroredirect).

The example below returns a 404 on a dynamic page after looking up an id in the database:
The example below looks up an ID in the database on a dynamic page and either it returns a 404 if the product does not exist, or it redirects the user to another page if the product is no longer available, or it displays the product:

```astro title="src/pages/[id].astro" {8-11}
```astro title="src/pages/[id].astro" {10-13, 18}
---
export const prerender = false; // Not needed in 'server' mode
import { getProduct } from '../api';
const product = await getProduct(Astro.params.id);
Expand All @@ -279,6 +283,11 @@ if (!product) {
statusText: 'Not found'
});
}
// The product is no longer available
if (!product.isAvailable) {
return Astro.redirect("/products", 301);
}
---
<html>
<!-- Page here... -->
Expand Down

0 comments on commit ce6715b

Please sign in to comment.