Understanding `force-dynamic` and `next build` in appDir
Unanswered
Araucanian herring posted this in #help-forum
Araucanian herringOP
I'm not sure I'm understanding how to correctly build a page with
I've tried
Clearly I'm not understanding the semantics here, but I would have expected that
Any guidance would be appreciated!
appDir if I need to refer to an external data source in a component referenced in my page.tsx file. Because my connection to my external data source is via an environment variable that is not set at build time, next build fails.I've tried
export const dynamic = 'force-dynamic' and export const revalidate = 0, and they don't do anything; next build still fails when it hits a page I'm telling it not to attempt to generate statically and node-getenv throws an exception due to the missing environment variable. If I set them to garbage values--because this application shouldn't be trying to access an external data source during build!--they obviously fail upon trying to connect to the nonexistent data source.Clearly I'm not understanding the semantics here, but I would have expected that
next build would skip these. Otherwise I have a really hard time understanding how I'm supposed to "just use server components" in the way that does totally work with next dev.Any guidance would be appreciated!
3 Replies
Northern Wheatear
During build nextjs will render all static pages and this will send requests to external data sources to do so. Why do you want to prevent it from accessing the data source
Araucanian herringOP
Because they're not static pages. They're inherently dynamic and environment-specifc. While they could be cached at render, I have another caching mechanism in place so I'm not worried about it (for now, and can optimize later). That's why I tried to tell NextJS this via
force-dynamic.I also tried the "call a method that's inherently dynamic to force it", and that didn't work either.