Add the `unstable_generateStaticVariants` page export
A page declares the variant combinations to prerender it against by
exporting `unstable_generateStaticVariants`. Each combination is a list
of `[variant, value]` tuples, and a combination does not have to assign
every variant the page reads:
```tsx
// app/page.tsx
import { locale, theme } from '../variants'
export function unstable_generateStaticVariants() {
return [
[[theme, 'dark']],
[
[theme, 'light'],
[locale, 'en'],
],
]
}
```
The name is prefixed because the shape is not settled. A page might
instead declare its combinations through `generateStaticParams`, or
through an API that does not exist yet, in which case this export goes
away rather than changes. The prefix is dropped once the shape is
settled.
Nothing prerenders against the combinations yet, so this change stops at
reading the export and rejecting what cannot be used. Every route still
resolves its variants for each request, and both callers discard the
result. A later change adds the prerendering.
Only a page may declare combinations. Prerenders are produced per page,
so combinations on a layout would multiply those of every page beneath
it, and a default page does not render the route. A route handler is
rejected for a different reason: it cannot read a variant yet, so its
response cannot differ between combinations. Within one combination each
variant takes a single value, and every value is a string.
Two combinations may assign different variants. One that assigns
everything another assigns and more is the more specific of the two,
which is how a page covers a route both broadly and narrowly. Two that
each assign a variant the other leaves out have no such order. The build
keeps such a pair when the two disagree on a variant they share, because
no single request then matches both, and rejects it when they agree
wherever they overlap.
An empty list is accepted. A list of combinations is often computed, and
a configuration that yields none should build rather than fail.
A mistake is reported where the export is read, so that it names the
route that made it. The build reads every app route, including one with
no dynamic segments, because such a route builds no static paths and
still declares combinations. The dev server reads a route where it
builds the static paths of that route, which happens on the first
request to a dynamic route.
Showing
Please
register
or
sign in
to comment