Next.js Discord

Discord Forum

Environment variables while runtime

Answered
Sun bear posted this in #help-forum
Open in Discord
Sun bearOP
Hello,

I wish to configure some parts of my application (auth server) in my environment where the nextjs application later runs.
For that I wanto to use env variables (as the most other application does too).
I read: https://nextjs.org/docs/pages/building-your-application/configuring/environment-variables#runtime-environment-variables
and I thought this should work.
For that I added noStore() to my client component:
"use client";

import React, { PropsWithChildren } from "react";

import { AuthProvider } from "react-oidc-context";

import { unstable_noStore as noStore } from "next/cache";
import { oidcConfig } from "./config";

export function AuthProviderWrapper({ children }: PropsWithChildren) {
  noStore();

  console.log(JSON.stringify(oidcConfig));
  return <AuthProvider {...oidcConfig}>{children}</AuthProvider>;
}


config:
"use server";

/**
 * The documentation about all properties is done here https://authts.github.io/oidc-client-ts/interfaces/UserManagerSettings.html#automaticSilentRenew
 */
export const oidcConfig = {
  authority: process.env.NEXT_PUBLIC_AUTH_AUTHORITY,
  client_id: process.env.NEXT_PUBLIC_AUTH_CLIENT_ID,
  redirect_uri: process.env.NEXT_PUBLIC_AUTH_REDIRECT_URI,
  loadUserInfo: true,
};

But that does not work. I see a console log with "undefined". If I remove the "user server" I see at least:
`
{"loadUserInfo":true}


After setting the env variables BEFORE the build proccess (npm run build) it works. But this is not the idea. I don't want to build my application for each context in which my application will run. I want to use one single build and configure it via ENV variables and don't want to do expensive CI/CD build proccesses to have multiple versions of my application with different configurations.
Answered by Sun bear
Okay for everybody, who is looking for a similar solution, I found a wunderful one. Just don't use the board tools of nextjs.
Use: https://github.com/expatfile/next-runtime-env

It is a little bit sad that we need a further library for such a main thing like application configuration. But what should I say?
View full answer

2 Replies

Sun bearOP
Is the question to wide or do you need some information to help or what exactly is the problem?
I would like to get an answere :/
Sun bearOP
Okay for everybody, who is looking for a similar solution, I found a wunderful one. Just don't use the board tools of nextjs.
Use: https://github.com/expatfile/next-runtime-env

It is a little bit sad that we need a further library for such a main thing like application configuration. But what should I say?
Answer