Precompute
Use feature flags on static pages.
Precomputing describes a pattern where Edge Middleware uses feature flags to decide which variant of a page to show. This allows you to keep the page itself static, which leads to incredibly low latency globally as the page can be served by a CDN or Edge Network.

With precompute, you can:
- Combine multiple feature flags on a single, static page.
- Use middleware to make routing decisions.
- Generate pages for each flag combination at build time or lazily, the first time it's accessed.
- Cache pages with Incremental Static Regeneration (ISR).
Precompute works by using dynamic route segments to transport an encoded version of the feature flags computed within Edge Middleware. Encoding the values within the URL allows the page itself to access the precomputed values, and also ensures there is a unique URL for each combination of feature flags on a page. Because the system works using rewrites, the visitor will never see the URL containing the flags. They will only see the clean, original URL.
Manual approach
It's possible to manually create variants of page by creating two versions of the same page. For example, app/home-a/page.tsx
and app/home-b/page.tsx
. Then, use Edge Middleware to rewrite the request either to /home-a
or /home-b
.
This approach works well for simple cases, but has a few downsides:
- It can be cumbersome having to maintain both
/home-a/page.tsx
and/home-b/page.tsx
. - It doesn't scale well when a feature flag is used on more than one page, or when multiple feature flags are used on a single page.
The precompute functionality of the Flags SDK can be used to work around these limitations.
Precompute pattern
1. Create flags to be precomputed
Export one or multiple flags as an array to be precomputed.
2. Precompute flags in middleware
Import and pass the group of flags to the precompute
function in middleware. Then, forward the precomputation result (code
) to the underlying page using an URL rewrite:
3. Access the precomputation result from a page
Next, import the feature flags you created earlier, such as showBanner
, while providing the code from the URL and the marketingFlags
list of flags used in the precomputation.
When the showBanner
flag is called within this component it reads the result from the precomputation, and it does not invoke the flag's decide
function again:
This approach allows middleware to decide the value of feature flags and to pass the precomputation result down to the page. This approach also works with API Routes.
Enabling ISR (optional)
You can enable Incremental Static Regeneration (ISR) to cache generated pages after their initial render:
In the example above, we used generateStaticParams
on the layout. You can also specify it on the page instead. It depends on whether a single page needs the flag or all pages within that layout need the flag.
Opting into build-time rendering (optional)
The flags/next
submodule exposes the generatePermutations
helper function for generating pages for different combinations of flags at build time. This function is called and takes a list of flags and returns an array of strings representing each combination of flags:
You can further customize which specific combinations you want render by passing a filter function as the second argument of generatePermutations
. And just like in the example above, you can also control whether you specify these permutations on the individual pages or on a layout.
Pages Router
If you're using the Pages Router, you need to pass a flag to generatePermutations
which accepts the code from context
and the group of flags.
You also need to specify a getStaticPaths
function which can return the permutations to generate at build time or an empty array to use ISR.
Declaring available options (optional)
Options are the possible values that a flag can take. You can declare the available options for a flag by passing an options
array to the flag
function:
Instead of passing the values directly you can also pass an object containing a label
and value
property:
To pass objects you must specify a label
and value
property:
The Flags SDK uses the declared options for multiple purposes:
-
Efficiently encode the flag values into the URL
The
precompute
function generates a short code which your appliciation then transports through the URL. The URLs must remain fairly short for the system to stay efficient. When a feature flag'sdecide
function returns a value not explicitly declared inoptions
the whole value needs to be inlined into thecode
which can quickly exceed the URL size limits. Especially for ISR the URL length needs to stay below 1024 characters. -
Generate the possible permutations flags
The
generatePermutations
function generates all possible combinations of flags for prerendering at build time. The function needs to know the available options for each flag to generate the possible permutations. It can only generate and prerender options decalred by the flag. -
Show the available options in the Flags Explorer
All options declared for a flag are shown in the Flags Explorer. If present, the
label
is used as the option name.