From a8f5b7940269c1344a02f0e1dc0831a28a08b92b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robin=20B=C3=BChler?= <1105080+openscript@users.noreply.github.com> Date: Mon, 15 Sep 2025 11:15:47 +0200 Subject: [PATCH 01/14] Document route data access in getStaticPaths with `routePattern` Add section on accessing route data and example code for getStaticPaths. --- .../docs/en/reference/routing-reference.mdx | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/content/docs/en/reference/routing-reference.mdx b/src/content/docs/en/reference/routing-reference.mdx index 3b304a94ae29c..3e88aaf30cd5e 100644 --- a/src/content/docs/en/reference/routing-reference.mdx +++ b/src/content/docs/en/reference/routing-reference.mdx @@ -170,6 +170,39 @@ const { post } = Astro.props;

{id}: {post.name}

``` +### Accessing route data + +

+ + +

+ +A string that can be returned from [`getStaticPaths()`](#getstaticpaths) to access the current [`routePattern`](/en/reference/api-reference/#routepattern). + +If a page `src/pages/[...locale]/[files]/[slug].astro` is being rendered, the corresponding `routePattern` is `[...locale]/[files]/[slug]`: + +Unlike `params`, which are concrete values for a page, `routePattern` always reflects the original dynamic segment definition in the file path. + +```astro title="src/pages/[...locale]/[files]/[slug].astro" +--- +export async function getStaticPaths({ routePattern }) { + // Calculate the appropriate params based on the routePattern + const params = []; + + // e.g. read route pattern and translate the [files] segment to all locales + if (routePattern.includes("[files]")) { + locales.forEach((locale) => { + params.push({ files: getMessage(locale, "files") }); + }); + } + + return { params }; +} + +const { page } = Astro.props; +--- +``` + ### `paginate()`

From fefad8ce8134f847717431106a2adc0dc4080dab Mon Sep 17 00:00:00 2001 From: Robin Date: Wed, 17 Sep 2025 11:04:15 +0200 Subject: [PATCH 02/14] chore: improve routePattern section title --- src/content/docs/en/reference/routing-reference.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/docs/en/reference/routing-reference.mdx b/src/content/docs/en/reference/routing-reference.mdx index 3e88aaf30cd5e..2c69314735c94 100644 --- a/src/content/docs/en/reference/routing-reference.mdx +++ b/src/content/docs/en/reference/routing-reference.mdx @@ -170,7 +170,7 @@ const { post } = Astro.props;

{id}: {post.name}

``` -### Accessing route data +### `routePattern`

From 3a4eec34e4f62b722603573ad22b701a40cba60e Mon Sep 17 00:00:00 2001 From: Robin Date: Wed, 17 Sep 2025 11:04:35 +0200 Subject: [PATCH 03/14] chore: set correct meta data for `routePattern` --- src/content/docs/en/reference/routing-reference.mdx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/content/docs/en/reference/routing-reference.mdx b/src/content/docs/en/reference/routing-reference.mdx index 2c69314735c94..e197f7c5abd31 100644 --- a/src/content/docs/en/reference/routing-reference.mdx +++ b/src/content/docs/en/reference/routing-reference.mdx @@ -174,7 +174,8 @@ const { post } = Astro.props;

- +**Type:** `string` +

A string that can be returned from [`getStaticPaths()`](#getstaticpaths) to access the current [`routePattern`](/en/reference/api-reference/#routepattern). From cfb93afb7909ab22e31d8de3ad0b34fb2a8f2016 Mon Sep 17 00:00:00 2001 From: Robin Date: Wed, 17 Sep 2025 11:07:18 +0200 Subject: [PATCH 04/14] chore: add line break --- src/content/docs/en/reference/routing-reference.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/docs/en/reference/routing-reference.mdx b/src/content/docs/en/reference/routing-reference.mdx index e197f7c5abd31..326b47aef78b0 100644 --- a/src/content/docs/en/reference/routing-reference.mdx +++ b/src/content/docs/en/reference/routing-reference.mdx @@ -174,7 +174,7 @@ const { post } = Astro.props;

-**Type:** `string` +**Type:** `string`

From b9d469f456c8822b0ccf80e212868d38c4813832 Mon Sep 17 00:00:00 2001 From: Robin Date: Wed, 17 Sep 2025 11:10:19 +0200 Subject: [PATCH 05/14] chore: improve `routePattern` description --- src/content/docs/en/reference/routing-reference.mdx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/content/docs/en/reference/routing-reference.mdx b/src/content/docs/en/reference/routing-reference.mdx index 326b47aef78b0..4ec72e2f68ea6 100644 --- a/src/content/docs/en/reference/routing-reference.mdx +++ b/src/content/docs/en/reference/routing-reference.mdx @@ -180,9 +180,9 @@ const { post } = Astro.props; A string that can be returned from [`getStaticPaths()`](#getstaticpaths) to access the current [`routePattern`](/en/reference/api-reference/#routepattern). -If a page `src/pages/[...locale]/[files]/[slug].astro` is being rendered, the corresponding `routePattern` is `[...locale]/[files]/[slug]`: +This provides data from the [Astro render context](https://docs.astro.build/en/reference/api-reference/) that would not otherwise be available within the scope of `getStaticPaths()` and can be useful to calculate the `params` and `props` for each page route. -Unlike `params`, which are concrete values for a page, `routePattern` always reflects the original dynamic segment definition in the file path. +`routePattern` always reflects the original dynamic segment definition in the file path (e.g. `/[...locale]/[slug]`), unlike `params`, which are explicit values for a page (e.g. `/fr/post-1/`). ```astro title="src/pages/[...locale]/[files]/[slug].astro" --- From 0412d33f0619634fb3c7bef44bc387583c1188ef Mon Sep 17 00:00:00 2001 From: Robin Date: Wed, 17 Sep 2025 13:14:51 +0200 Subject: [PATCH 06/14] chore: clearify parameter --- src/content/docs/en/reference/routing-reference.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/docs/en/reference/routing-reference.mdx b/src/content/docs/en/reference/routing-reference.mdx index 4ec72e2f68ea6..3a19892fc5a6f 100644 --- a/src/content/docs/en/reference/routing-reference.mdx +++ b/src/content/docs/en/reference/routing-reference.mdx @@ -178,7 +178,7 @@ const { post } = Astro.props;

-A string that can be returned from [`getStaticPaths()`](#getstaticpaths) to access the current [`routePattern`](/en/reference/api-reference/#routepattern). +A string parameter [`getStaticPaths()`](#getstaticpaths) to access the current [`routePattern`](/en/reference/api-reference/#routepattern). This provides data from the [Astro render context](https://docs.astro.build/en/reference/api-reference/) that would not otherwise be available within the scope of `getStaticPaths()` and can be useful to calculate the `params` and `props` for each page route. From 5f60ec3e60961c6ef9689af47cefa92b393125bd Mon Sep 17 00:00:00 2001 From: Robin Date: Wed, 17 Sep 2025 16:17:57 +0200 Subject: [PATCH 07/14] chore: fix Astro render context link --- src/content/docs/en/reference/routing-reference.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/docs/en/reference/routing-reference.mdx b/src/content/docs/en/reference/routing-reference.mdx index 3a19892fc5a6f..3acb4e9dbccd6 100644 --- a/src/content/docs/en/reference/routing-reference.mdx +++ b/src/content/docs/en/reference/routing-reference.mdx @@ -180,7 +180,7 @@ const { post } = Astro.props; A string parameter [`getStaticPaths()`](#getstaticpaths) to access the current [`routePattern`](/en/reference/api-reference/#routepattern). -This provides data from the [Astro render context](https://docs.astro.build/en/reference/api-reference/) that would not otherwise be available within the scope of `getStaticPaths()` and can be useful to calculate the `params` and `props` for each page route. +This provides data from the [Astro render context](/en/reference/api-reference/) that would not otherwise be available within the scope of `getStaticPaths()` and can be useful to calculate the `params` and `props` for each page route. `routePattern` always reflects the original dynamic segment definition in the file path (e.g. `/[...locale]/[slug]`), unlike `params`, which are explicit values for a page (e.g. `/fr/post-1/`). From 690d6cdbe7c3f43da1a25f977c233972766ab1e7 Mon Sep 17 00:00:00 2001 From: Robin Date: Wed, 17 Sep 2025 16:20:52 +0200 Subject: [PATCH 08/14] chore: replace example --- .../docs/en/reference/routing-reference.mdx | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/content/docs/en/reference/routing-reference.mdx b/src/content/docs/en/reference/routing-reference.mdx index 3acb4e9dbccd6..1ee0d537a4ad2 100644 --- a/src/content/docs/en/reference/routing-reference.mdx +++ b/src/content/docs/en/reference/routing-reference.mdx @@ -186,21 +186,20 @@ This provides data from the [Astro render context](/en/reference/api-reference/) ```astro title="src/pages/[...locale]/[files]/[slug].astro" --- +import { getLocalizedData } from "../../../utils/i18n"; + export async function getStaticPaths({ routePattern }) { - // Calculate the appropriate params based on the routePattern - const params = []; + const response = await fetch('...'); + const data = await response.json(); - // e.g. read route pattern and translate the [files] segment to all locales - if (routePattern.includes("[files]")) { - locales.forEach((locale) => { - params.push({ files: getMessage(locale, "files") }); - }); - } + console.log(routePattern); // [...locale]/[files]/[slug] - return { params }; + // Your custom helper will be responsible of parsing the `routePattern` value to return an array of objects matching + // `{ params: { locale: "en", files: "localized-string", slug: "localized-slug" }, props: { /* your data */ } }` + return data.flatMap((file) => getLocalizedData(file, routePattern)); } -const { page } = Astro.props; +const { locale, files, slug } = Astro.params; --- ``` From 143362f4fcd552bc2b632bf4ef86c122943b99d6 Mon Sep 17 00:00:00 2001 From: Robin Date: Wed, 17 Sep 2025 16:21:43 +0200 Subject: [PATCH 09/14] chore: enhance example --- src/content/docs/en/reference/routing-reference.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/docs/en/reference/routing-reference.mdx b/src/content/docs/en/reference/routing-reference.mdx index 1ee0d537a4ad2..02bd7770b9c6d 100644 --- a/src/content/docs/en/reference/routing-reference.mdx +++ b/src/content/docs/en/reference/routing-reference.mdx @@ -194,7 +194,7 @@ export async function getStaticPaths({ routePattern }) { console.log(routePattern); // [...locale]/[files]/[slug] - // Your custom helper will be responsible of parsing the `routePattern` value to return an array of objects matching + // Your custom helper will be responsible of parsing the `routePattern` value to return an array (for each locale) of objects matching // `{ params: { locale: "en", files: "localized-string", slug: "localized-slug" }, props: { /* your data */ } }` return data.flatMap((file) => getLocalizedData(file, routePattern)); } From 326baa29d823c095261323115b2d0cf831caf9ea Mon Sep 17 00:00:00 2001 From: Robin Date: Thu, 18 Sep 2025 15:47:46 +0200 Subject: [PATCH 10/14] chore: incorperate feedback --- src/content/docs/en/reference/routing-reference.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/docs/en/reference/routing-reference.mdx b/src/content/docs/en/reference/routing-reference.mdx index 02bd7770b9c6d..33a2cad408a23 100644 --- a/src/content/docs/en/reference/routing-reference.mdx +++ b/src/content/docs/en/reference/routing-reference.mdx @@ -178,7 +178,7 @@ const { post } = Astro.props;

-A string parameter [`getStaticPaths()`](#getstaticpaths) to access the current [`routePattern`](/en/reference/api-reference/#routepattern). +A property available in [`getStaticPaths()`](#getstaticpaths) options to access the current [`routePattern`](/en/reference/api-reference/#routepattern) as a string. This provides data from the [Astro render context](/en/reference/api-reference/) that would not otherwise be available within the scope of `getStaticPaths()` and can be useful to calculate the `params` and `props` for each page route. From 0875ddb32e37dca49700fd771808baeb1f26153e Mon Sep 17 00:00:00 2001 From: Sarah Rainsberger <5098874+sarah11918@users.noreply.github.com> Date: Thu, 18 Sep 2025 15:26:05 -0300 Subject: [PATCH 11/14] introduce example --- src/content/docs/en/reference/routing-reference.mdx | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/content/docs/en/reference/routing-reference.mdx b/src/content/docs/en/reference/routing-reference.mdx index 33a2cad408a23..cf24d312958c1 100644 --- a/src/content/docs/en/reference/routing-reference.mdx +++ b/src/content/docs/en/reference/routing-reference.mdx @@ -184,7 +184,10 @@ This provides data from the [Astro render context](/en/reference/api-reference/) `routePattern` always reflects the original dynamic segment definition in the file path (e.g. `/[...locale]/[slug]`), unlike `params`, which are explicit values for a page (e.g. `/fr/post-1/`). -```astro title="src/pages/[...locale]/[files]/[slug].astro" +The following example shows how to localize your route segments and return an array of static paths by passing `routePattern` to a custom `getLocalizedData()` helper function. The [params](https://github.com/en/reference/routing-reference/#params) object will be set with explicit values for each route segment: `locale`, `files`, and `slug`. Then, these values will be used to generate the routes and can be used in your page template via `Astro.params`. + + +```astro title="src/pages/[...locale]/[files]/[slug].astro" "routePattern" "getLocalizedData" --- import { getLocalizedData } from "../../../utils/i18n"; From 9bce933c3f642f4bef902a7625b8802bc7f691f6 Mon Sep 17 00:00:00 2001 From: Sarah Rainsberger <5098874+sarah11918@users.noreply.github.com> Date: Thu, 18 Sep 2025 15:26:27 -0300 Subject: [PATCH 12/14] simplify code comment --- src/content/docs/en/reference/routing-reference.mdx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/content/docs/en/reference/routing-reference.mdx b/src/content/docs/en/reference/routing-reference.mdx index cf24d312958c1..75f44d494c048 100644 --- a/src/content/docs/en/reference/routing-reference.mdx +++ b/src/content/docs/en/reference/routing-reference.mdx @@ -197,8 +197,7 @@ export async function getStaticPaths({ routePattern }) { console.log(routePattern); // [...locale]/[files]/[slug] - // Your custom helper will be responsible of parsing the `routePattern` value to return an array (for each locale) of objects matching - // `{ params: { locale: "en", files: "localized-string", slug: "localized-slug" }, props: { /* your data */ } }` + // Call your custom helper with `routePattern` to generate the static paths return data.flatMap((file) => getLocalizedData(file, routePattern)); } From 7e6cbbd58aaf719d1cf6001e721d451a8a95209d Mon Sep 17 00:00:00 2001 From: Sarah Rainsberger <5098874+sarah11918@users.noreply.github.com> Date: Thu, 18 Sep 2025 16:48:40 -0300 Subject: [PATCH 13/14] explicit path names to match the `locale` --- src/content/docs/en/reference/routing-reference.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/docs/en/reference/routing-reference.mdx b/src/content/docs/en/reference/routing-reference.mdx index 75f44d494c048..4957865729331 100644 --- a/src/content/docs/en/reference/routing-reference.mdx +++ b/src/content/docs/en/reference/routing-reference.mdx @@ -182,7 +182,7 @@ A property available in [`getStaticPaths()`](#getstaticpaths) options to access This provides data from the [Astro render context](/en/reference/api-reference/) that would not otherwise be available within the scope of `getStaticPaths()` and can be useful to calculate the `params` and `props` for each page route. -`routePattern` always reflects the original dynamic segment definition in the file path (e.g. `/[...locale]/[slug]`), unlike `params`, which are explicit values for a page (e.g. `/fr/post-1/`). +`routePattern` always reflects the original dynamic segment definition in the file path (e.g. `/[...locale]/[files]/[slug]`), unlike `params`, which are explicit values for a page (e.g. `/fr/fichiers/article-1/`). The following example shows how to localize your route segments and return an array of static paths by passing `routePattern` to a custom `getLocalizedData()` helper function. The [params](https://github.com/en/reference/routing-reference/#params) object will be set with explicit values for each route segment: `locale`, `files`, and `slug`. Then, these values will be used to generate the routes and can be used in your page template via `Astro.params`. From d68dec0fecf1a116ee565577105a77f9b2289ace Mon Sep 17 00:00:00 2001 From: Robin Date: Mon, 22 Sep 2025 15:26:23 +0200 Subject: [PATCH 14/14] chore: update version --- src/content/docs/en/reference/routing-reference.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/docs/en/reference/routing-reference.mdx b/src/content/docs/en/reference/routing-reference.mdx index 4957865729331..39882014a6cda 100644 --- a/src/content/docs/en/reference/routing-reference.mdx +++ b/src/content/docs/en/reference/routing-reference.mdx @@ -175,7 +175,7 @@ const { post } = Astro.props;

**Type:** `string`
- +

A property available in [`getStaticPaths()`](#getstaticpaths) options to access the current [`routePattern`](/en/reference/api-reference/#routepattern) as a string.