Skip to content

getStaticPaths

getStaticPaths is a bit special in Astro. On one hand it’s absolutely necessary for dynamic SSG routes but it runs before everything, even middlewares.

Under the hood, the package uses a middleware so we can’t access any of the standard utilities inside of getStaticPaths. Instead, we provide placeholder functions that are build time macros: getLocalePlaceholder, getLocalesPlaceholder and getDefaultLocalePlaceholder. That means it will be replaced by it’s literal value. For instance:

1
---
2
import { getLocalePlaceholder, getLocalesPlaceholder, getDefaultLocalePlaceholder } from "i18n:astro"
3
4
export const getStaticPaths = () => {
5
const locale = getLocalePlaceholder()
6
const defaultLocale = getDefaultLocalePlaceholder()
7
const locales = getLocalesPlaceholder()
8
9
return []
10
}
11
---

Will be replaced by the following, no matter the context:

1
---
2
import { getLocalePlaceholder, getLocalesPlaceholder, getDefaultLocalePlaceholder } from "i18n:astro"
3
4
export const getStaticPaths = () => {
5
const locale = "en"
6
const defaultLocale = "en"
7
const locales = ["en", "fr"]
8
9
return []
10
}
11
---

Be careful not to use these functions in interpolations or it could result in invalid code.