From ce6715bcb3aeafbdf767cb6408031dd821b1c617 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Wed, 16 Oct 2024 17:02:26 +0200 Subject: [PATCH] update a code snippet in `guides/server-side-rendering.mdx` (#9700) Co-authored-by: Sarah Rainsberger --- .../docs/en/guides/server-side-rendering.mdx | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/content/docs/en/guides/server-side-rendering.mdx b/src/content/docs/en/guides/server-side-rendering.mdx index 42713a5e9055a..0def1086f8a27 100644 --- a/src/content/docs/en/guides/server-side-rendering.mdx +++ b/src/content/docs/en/guides/server-side-rendering.mdx @@ -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); @@ -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); @@ -279,6 +283,11 @@ if (!product) { statusText: 'Not found' }); } + +// The product is no longer available +if (!product.isAvailable) { + return Astro.redirect("/products", 301); +} ---