Next.js Discord

Discord Forum

App router - opt out of static optimization?

Unanswered
Cape lion posted this in #help-forum
Open in Discord
Cape lionOP
I am trying to access server runtime process variables and pass them to client. To do this I have a function in layout.tsx which works when I have it fully dynamic (using route segment config, or a blocking fetch call etc). I don't want it to be dynamic because runtime process variables won't change. I'd like it to be cached and run once.

If I remove the dynamic nature it gets pre- rendered at build time. Then the process will be the client side process and won't have the variables I'm trying to access.

Can I opt-out of static optimization without opting out of caching?

async function getServerProcessEnv() {
  return process.env;
}

export default async function RootLayout(){
  const serverEnv = await getServerProcessEnv();
 return {....}
  );
}

0 Replies