Next.js Discord

Discord Forum

Conditionally switch between dynamic and static pages during build time.

Unanswered
Red-shouldered Hawk posted this in #help-forum
Open in Discord
Avatar
Red-shouldered HawkOP
Hi All,

I'm trying to have the same pages built statically output: export (hosted on s3) for production but the business wants to have a "draft" website output: standalone. This is so content editors can see the full website when making changes in the CMS instead of just using draft mode which is limited to one page (as far as I can tell).

The only way i've gotten this to work is using export const dynamic = process.env.DRAFT === 'true' ? "force-dynamic" : "force-static".
But even though that works during the build process it throws an error in my IDE The configuration must be statically analyzable which is annoying.

Is there another way I could possibly do this? I'm kind of locked into Next14 at the moment because of CMS dependencies throwing errors with react19.

page.tsx
/** --- PAGE SETUP --- */
export const dynamic = process.env.DRAFT === 'true' ? "force-dynamic" : "force-static"

export async function generateStaticParams() {
  // Skip static generation in development/preview
  if (process.env.DRAFT === 'true') {
    return [];
  }

  try {
    const generator = new StaticParamsGenerator(client)

    const params = await generator.generateParams({
      contentType: 'basicPageTemplate',
      websiteId: 'Explore'
    });

    return params;
  } catch (error) {
    console.error('Error in generateStaticParams:', error);
    return [];
  }
}

....

export default async function Page({ params }: { params: { slug: string[] }}) {
  console.log("GET PAGE")
  const basicPageData = await getPageData(params.slug)

  return (
    <BasicPage {...basicPageData} />
  )
}

1 Reply

Avatar
Red-shouldered HawkOP
const dynamic = process.env.DRAFT === 'true' ? "force-dynamic" : "force-static"
export { dynamic }

This seems to work and hides the IDE error. I'm gonna keep this open for a bit longer to see there is a better way to do it.