How can I build my app without pre-rendering pages? (app router)
Unanswered
Bonga shad posted this in #help-forum
Bonga shadOP
I would like to create my app build it fails to build if my API is not running. Is that because Next.js by default tries to prerender the initial page? How can I opt out so that each request is fresh (nothing prerendered)? Or thats not recommended and I am missing something ... I am using Next 14 with App router.
8 Replies
@Bonga shad I would like to create my app build it fails to build if my API is not running. Is that because Next.js by default tries to prerender the initial page? How can I opt out so that each request is fresh (nothing prerendered)? Or thats not recommended and I am missing something ... I am using Next 14 with App router.
that's not recommended. don't call your own route handlers in server components. https://nextjs-faq.com/fetch-api-in-rsc
Bonga shadOP
@joulev thanks for quick answer. I am using external API rather than next api routes, does it make any difference?
@Bonga shad <@484037068239142956> thanks for quick answer. I am using external API rather than next api routes, does it make any difference?
oh then it is different. restructure your workflow in such a way that guarantees the api is available during build. you don't simply take your api down whenever you build the app right?
Bonga shadOP
yeah, that's probably the way to go. Thanks!
@joulev just out of curiosity what is the reason for failing to build? Is this because next.js is trying to prerender the page for the faster initial load?
@Bonga shad <@484037068239142956> just out of curiosity what is the reason for failing to build? Is this because next.js is trying to prerender the page for the faster initial load?
yup. the page content is fetched and rendered at build time (static rendering). requests can simply consume the prebuilt content so it is significantly faster.
Bonga shadOP
thanks for the answer. Then if data has changed since the build would they still get the static rendered page? or next js is smart enough to check that data has changed and will server updated one?
@Bonga shad thanks for the answer. Then if data has changed since the build would they still get the static rendered page? or next js is smart enough to check that data has changed and will server updated one?
for static rendering, no, nextjs has no way to know the data has changed. you have to explicitly tell it to revalidate.
if you want to use dynamic rendering (nextjs doesn't cache anything and instead just fetch everything at every request, which is a bit slower), then nextjs will get you fresh data every time. to do that, add
if you want to use dynamic rendering (nextjs doesn't cache anything and instead just fetch everything at every request, which is a bit slower), then nextjs will get you fresh data every time. to do that, add
export const revalidate = 0 to the page.