4

I have a NEXT.JS project and I want to deploy it to 3 different environments. For example: SIT, UAT, PROD, all these 3 environments have different configuration values like API_URL and API_KEY which must be changed accordingly. So I have to build the project again and again every time I want to deploy to another environment because by default NEXT.JS read those values at build time which can't be changed once the project is already built. In this project I use Pages router.

I want to build it just one time and deploy it to anywhere by just changing the environment values in .env file following the The Twelve-Factor-App methodology. For instance, I can do that with my spring boot project where I just build it one time and then when I want to deploy to other environments I just need to change the environment values in .env file and restart the app but with NEXT.JS, it seems to be tricky.

1
  • Could you share us a hello world with next to try? Commented Jul 3, 2024 at 4:59

1 Answer 1

1

Env variable = Backend

Since you are talking about env variables (which is good instead hard coded values), react should discarded for simplicity. I mean the variable set should be performed at the backed layer (nodejs). In your example, the API_URL and API_KEY should not be used at the react layer, instead of that, at the backend layer. Something like:

enter image description here

So the next.js backend should use the env variables to do whatever is needed and then return the data to the react layer using the same endpoint. So if you deploy to another server, just change the env variables at backend and assign another domain.

AJAX

Remember that at the frontend layer (react) for security you should not use static variables because as you mentioned, they will be inside of the js build. Only short live tokens (oauth2) should ve used at the frontend layer

Anyway, if you are forced to use the API_URL and API_KEY at the frontend layer (react), you can use these approach:

  • Create one endpoint in the next.js backend called /spa-settings
  • This endpoint have access to the os in the backend so is able to read any env variable
  • Return the required env variables as a json response of /spa-settings
{
  "API_URL":"www.foo-api.com/productos",
  "API_KEY":"e3c06b64-f242-4ca7"
}
  • If you will have several en variables to be exposed you could use the approach of CRA in which all the prefixed variables with REACT_APP_ are exposed.
  • Read these value at the entrypoint of react application and expose the values to all the react files (javascript) using any strategy like localstorage, state , global variables, etc
import axios from "axios";
import { createRoot } from 'react-dom/client';
import React from "react";
import App from "./App";
import "./index.css";
import "bootstrap/dist/css/bootstrap.css";

axios.get("./settings.json").then(response => response.data)
.then((data) => {
  console.log("settings:", JSON.stringify(data))
  localStorage.setItem('settings', JSON.stringify(data));
  const container = document.getElementById('root');
  const root = createRoot(container); 
  root.render(<App />);
});
  • Then on any react file you will be able to access the variables

1 build, N deploys

With these approaches you will need to build your application just one time and the deploy anywhere just setting the required env variables before the startup

References

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.