Next.js Discord

Discord Forum

Skipping force-dynamic handlers during static export / Conditionally setting dynamic

Answered
Sun bear posted this in #help-forum
Open in Discord
Sun bearOP
Hello,

I have a web app that builds with output: 'standalone' or output: 'export' depending on an envar:
const nextConfig = {
  output: process.env.STANDALONE ? 'standalone' : 'export'
  // ...
}

In the standalone case, I have some page handlers that require export const dynamic = 'force-dynamic' because they do things like load data from supabase (if I don't force dynamic, then it complains about DynamicServerError: Dynamic server usage: cookies for example).

As expected, this results in an error during my export build:
Page with `dynamic = "force-dynamic"` couldn't be rendered statically because it used `output: export`

Ideally I would do something like this:
export const dynamic = process.env.STANDALONE ? 'force-dynamic' : 'auto'

But this is not statically analyzable, so it fails.

How can I set this up to support both standalone with dynamic routes, and export without dynamic routes?
Answered by Sun bear
It looks like it just works when I do
export const dynamic = process.env.STANDALONE ? 'force-dynamic' : 'auto'

Despite the typescript (or eslint?) warning about not being statically analyzable and the [documentation](https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config).

The warning is a bit strange, but given that the envar is available at build time it seems to work as intended for each respective build :ablobgrimace:
View full answer

1 Reply

Sun bearOP
It looks like it just works when I do
export const dynamic = process.env.STANDALONE ? 'force-dynamic' : 'auto'

Despite the typescript (or eslint?) warning about not being statically analyzable and the [documentation](https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config).

The warning is a bit strange, but given that the envar is available at build time it seems to work as intended for each respective build :ablobgrimace:
Answer