Next.js Discord

Discord Forum

How to Add custom env

Unanswered
Lazuee posted this in #help-forum
Open in Discord
I want to put gitpod current base url on port
import { $ } from "zx";

if (process.env?.GITPOD_WORKSPACE_URL && !process.env?.NEXT_PUBLIC_GITPOD_URL)
    (process.env as any).NEXT_PUBLIC_GITPOD_URL = (await $`gp url 3000`).stdout;

I want it to run once,. not every reload of the web page.

3 Replies

where can i put it to run it once? and to be able to access it every where?
I tried putting it on next.config.js but it didn't call the command..
Persian
My next.config.js file look like this:
module.exports = {
publicRuntimeConfig: {
BACKEND_URL: process.env.BACKEND_URL
}
};

And then in code:
import getConfig from 'next/config';
const { publicRuntimeConfig } = getConfig();

....

return await fetch(${publicRuntimeConfig.BACKEND_URL}/xxx....