Skip to content

Commit a31edb8

Browse files
openscriptsarah11918florian-lefebvre
authored
feat(core): add routePattern to GetStaticPathsOptions (#13520)
* feat: add `routePattern` to `GetStaticPathsOptions` * chore: add changeset * chore: test `routePattern` in `getStaticPaths()` * chore: enhance test * fill out changeset with hype! --------- Co-authored-by: Sarah Rainsberger <[email protected]> Co-authored-by: Florian Lefebvre <[email protected]>
1 parent 69572c0 commit a31edb8

File tree

5 files changed

+52
-10
lines changed

5 files changed

+52
-10
lines changed

.changeset/little-sheep-hear.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
'astro': minor
3+
---
4+
5+
Adds a new property `routePattern` available to `GetStaticPathsOptions`
6+
7+
This provides the original, dynamic segment definition in a routing file path (e.g. `/[...locale]/[files]/[slug]`) from the Astro render context that would not otherwise be available within the scope of `getStaticPaths()`. This can be useful to calculate the `params` and `props` for each page route.
8+
9+
For example, you can now localize your route segments and return an array of static paths by passing `routePattern` to a custom `getLocalizedData()` helper function. The `params` object will be set with explicit values for each route segment (e.g. `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`.
10+
11+
12+
```astro
13+
// src/pages/[...locale]/[files]/[slug].astro
14+
---
15+
import { getLocalizedData } from "../../../utils/i18n";
16+
export async function getStaticPaths({ routePattern }) {
17+
const response = await fetch('...');
18+
const data = await response.json();
19+
console.log(routePattern); // [...locale]/[files]/[slug]
20+
// Call your custom helper with `routePattern` to generate the static paths
21+
return data.flatMap((file) => getLocalizedData(file, routePattern));
22+
}
23+
const { locale, files, slug } = Astro.params;
24+
---
25+
```
26+
27+
For more information about this advanced routing pattern, see Astro's [routing reference](https://docs.astro.build/en/reference/routing-reference/#routepattern).

packages/astro/src/core/render/route-cache.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export async function callGetStaticPaths({
6060
// Q: Why the cast?
6161
// A: So users downstream can have nicer typings, we have to make some sacrifice in our internal typings, which necessitate a cast here
6262
paginate: generatePaginateFunction(route, base) as PaginateFunction,
63+
routePattern: route.route,
6364
});
6465

6566
validateGetStaticPathsResult(staticPaths, logger, route);

packages/astro/src/types/public/common.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type { APIContext } from './context.js';
99
*/
1010
export interface GetStaticPathsOptions {
1111
paginate: PaginateFunction;
12+
routePattern: string;
1213
}
1314

1415
export type GetStaticPathsItem = {

packages/astro/test/astro-get-static-paths.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,16 @@ describe('getStaticPaths - dev calls', () => {
103103
});
104104
});
105105

106+
it('provides routePattern', async () => {
107+
const res = await fixture.fetch('/blog/2022/post-1');
108+
assert.equal(res.status, 200);
109+
110+
const html = await res.text();
111+
const $ = cheerio.load(html);
112+
113+
assert.equal($('#route-pattern').text(), '/blog/[year]/[slug]');
114+
});
115+
106116
it('resolves 200 on matching static paths', async () => {
107117
// routes params provided for pages /posts/1, /posts/2, and /posts/3
108118
for (const page of [1, 2, 3]) {
Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
---
2-
export async function getStaticPaths() {
3-
return [
4-
{ params: { year: '2022', slug: 'post-1' } },
5-
{ params: { year: 2022, slug: 'post-2' } },
6-
{ params: { slug: 'post-2', year: '2022' } },
7-
]
2+
export async function getStaticPaths({ routePattern }) {
3+
return [
4+
{ params: { year: '2022', slug: 'post-1' }, props: { routePattern } },
5+
{ params: { year: 2022, slug: 'post-2' }, props: { routePattern } },
6+
{ params: { slug: 'post-2', year: '2022' }, props: { routePattern } },
7+
]
88
}
99
1010
const { year, slug } = Astro.params
11+
const { routePattern } = Astro.props
1112
---
1213

1314
<html>
14-
<head>
15-
<title>{year} | {slug}</title>
16-
</head>
17-
<body></body>
15+
<head>
16+
<title>{year} | {slug}</title>
17+
</head>
18+
<body>
19+
<p id="route-pattern">{routePattern}</p>
20+
</body>
1821
</html>

0 commit comments

Comments
 (0)