How to inject runtime env vars to your Next.js app

It is very often beneficial to load environment variables during runtime to your client side, for example, when you want a single Docker image to use in different environments. This would not be possible when using build-time environments (NEXT_PUBLIC_…


This content originally appeared on DEV Community and was authored by Michał Wrzosek

It is very often beneficial to load environment variables during runtime to your client side, for example, when you want a single Docker image to use in different environments. This would not be possible when using build-time environments (NEXT_PUBLIC_...). Surprisingly, it may not be that obvious how to do so...

Here's one way you can do it:

You have your dynamic layout that will be executed on the server side that injects the env vars config to the client side. You can't use the "use client" clause here.

/src/app/layout.tsx

import { PropsWithChildren } from 'react';
import { unstable_noStore } from 'next/cache';
import { LayoutClient } from './layout-client';

export default function Layout({ children }: PropsWithChildren) {
  // This is important!
  unstable_noStore();

  // some example config to be injected to the client side
  const config = {
    apiEndpoint: process.env.API_ENDPOINT!,
    appEnvironment: process.env.APP_ENVIRONMENT!,
  };

  return (
    <html lang="en">
      <head>
        <link rel="icon" href="favicon.ico" />
      </head>
      <body>
        <LayoutClient config={config}>{children}</LayoutClient>
      </body>
    </html>
  )
}

Then you have your "client" layout:

/src/app/layout-client.tsx

'use client';

import { PropsWithChildren } from 'react';

export const LayoutClient = ({ config, children }: PropsWithChildren<{
  apiEndpoint: string;
  appEnvironment: string;
}>) => {
  // and here you are now on the "client" and basically,
  // you can do whatever you want with your config
}

I split the layout into "server" and "client" because I like to push the config through the React Context so that it will be available to all the components and this of course requires you to be within the "use client" clause.

In short, to make this work you need "unstable_noStore" and careful "use client" clause placement.


This content originally appeared on DEV Community and was authored by Michał Wrzosek


Print Share Comment Cite Upload Translate Updates
APA

Michał Wrzosek | Sciencx (2024-10-17T23:07:04+00:00) How to inject runtime env vars to your Next.js app. Retrieved from https://www.scien.cx/2024/10/17/how-to-inject-runtime-env-vars-to-your-next-js-app/

MLA
" » How to inject runtime env vars to your Next.js app." Michał Wrzosek | Sciencx - Thursday October 17, 2024, https://www.scien.cx/2024/10/17/how-to-inject-runtime-env-vars-to-your-next-js-app/
HARVARD
Michał Wrzosek | Sciencx Thursday October 17, 2024 » How to inject runtime env vars to your Next.js app., viewed ,<https://www.scien.cx/2024/10/17/how-to-inject-runtime-env-vars-to-your-next-js-app/>
VANCOUVER
Michał Wrzosek | Sciencx - » How to inject runtime env vars to your Next.js app. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/10/17/how-to-inject-runtime-env-vars-to-your-next-js-app/
CHICAGO
" » How to inject runtime env vars to your Next.js app." Michał Wrzosek | Sciencx - Accessed . https://www.scien.cx/2024/10/17/how-to-inject-runtime-env-vars-to-your-next-js-app/
IEEE
" » How to inject runtime env vars to your Next.js app." Michał Wrzosek | Sciencx [Online]. Available: https://www.scien.cx/2024/10/17/how-to-inject-runtime-env-vars-to-your-next-js-app/. [Accessed: ]
rf:citation
» How to inject runtime env vars to your Next.js app | Michał Wrzosek | Sciencx | https://www.scien.cx/2024/10/17/how-to-inject-runtime-env-vars-to-your-next-js-app/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.