Skip to content

Commit 554bc55

Browse files
openscriptsarah11918florian-lefebvreArmandPhilippot
authored
Add "Accessing route data" section in getStaticPaths with routePattern (#12316)
Co-authored-by: Sarah Rainsberger <[email protected]> Co-authored-by: florian-lefebvre <[email protected]> Co-authored-by: ArmandPhilippot <[email protected]>
1 parent bd3cdf0 commit 554bc55

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/content/docs/en/reference/routing-reference.mdx

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,41 @@ const { post } = Astro.props;
170170
<h1>{id}: {post.name}</h1>
171171
```
172172

173+
### `routePattern`
174+
175+
<p>
176+
177+
**Type:** `string` <br />
178+
<Since v="5.14.0" />
179+
</p>
180+
181+
A property available in [`getStaticPaths()`](#getstaticpaths) options to access the current [`routePattern`](/en/reference/api-reference/#routepattern) as a string.
182+
183+
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.
184+
185+
`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/`).
186+
187+
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`.
188+
189+
190+
```astro title="src/pages/[...locale]/[files]/[slug].astro" "routePattern" "getLocalizedData"
191+
---
192+
import { getLocalizedData } from "../../../utils/i18n";
193+
194+
export async function getStaticPaths({ routePattern }) {
195+
const response = await fetch('...');
196+
const data = await response.json();
197+
198+
console.log(routePattern); // [...locale]/[files]/[slug]
199+
200+
// Call your custom helper with `routePattern` to generate the static paths
201+
return data.flatMap((file) => getLocalizedData(file, routePattern));
202+
}
203+
204+
const { locale, files, slug } = Astro.params;
205+
---
206+
```
207+
173208
### `paginate()`
174209

175210
<p>

0 commit comments

Comments
 (0)