Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .changeset/little-sheep-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
'astro': minor
---

Adds a new property `routePattern` available to `GetStaticPathsOptions`

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.

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`.


```astro
// src/pages/[...locale]/[files]/[slug].astro
---
import { getLocalizedData } from "../../../utils/i18n";
export async function getStaticPaths({ routePattern }) {
const response = await fetch('...');
const data = await response.json();
console.log(routePattern); // [...locale]/[files]/[slug]
// Call your custom helper with `routePattern` to generate the static paths
return data.flatMap((file) => getLocalizedData(file, routePattern));
}
const { locale, files, slug } = Astro.params;
---
```

For more information about this advanced routing pattern, see Astro's [routing reference](https://docs.astro.build/en/reference/routing-reference/#routepattern).
1 change: 1 addition & 0 deletions packages/astro/src/core/render/route-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export async function callGetStaticPaths({
// Q: Why the cast?
// A: So users downstream can have nicer typings, we have to make some sacrifice in our internal typings, which necessitate a cast here
paginate: generatePaginateFunction(route, base) as PaginateFunction,
routePattern: route.route,
});

validateGetStaticPathsResult(staticPaths, logger, route);
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/types/public/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { APIContext } from './context.js';
*/
export interface GetStaticPathsOptions {
paginate: PaginateFunction;
routePattern: string;
}

export type GetStaticPathsItem = {
Expand Down
10 changes: 10 additions & 0 deletions packages/astro/test/astro-get-static-paths.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ describe('getStaticPaths - dev calls', () => {
});
});

it('provides routePattern', async () => {
const res = await fixture.fetch('/blog/2022/post-1');
assert.equal(res.status, 200);

const html = await res.text();
const $ = cheerio.load(html);

assert.equal($('#route-pattern').text(), '/blog/[year]/[slug]');
});

it('resolves 200 on matching static paths', async () => {
// routes params provided for pages /posts/1, /posts/2, and /posts/3
for (const page of [1, 2, 3]) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
---
export async function getStaticPaths() {
return [
{ params: { year: '2022', slug: 'post-1' } },
{ params: { year: 2022, slug: 'post-2' } },
{ params: { slug: 'post-2', year: '2022' } },
]
export async function getStaticPaths({ routePattern }) {
return [
{ params: { year: '2022', slug: 'post-1' }, props: { routePattern } },
{ params: { year: 2022, slug: 'post-2' }, props: { routePattern } },
{ params: { slug: 'post-2', year: '2022' }, props: { routePattern } },
]
}

const { year, slug } = Astro.params
const { routePattern } = Astro.props
---

<html>
<head>
<title>{year} | {slug}</title>
</head>
<body></body>
<head>
<title>{year} | {slug}</title>
</head>
<body>
<p id="route-pattern">{routePattern}</p>
</body>
</html>
Loading