Becksholm
Becksholm

Reputation: 1

How do you set dokku environment variables for a Nextjs application

I have a Nextjs application deployed on dokku but it does not find dokku environment variables. I have also tried to read the environment variable from a Next api route and still return nothing.

Next api route

export async function GET() {
  const baseUrl = process.env.NEXT_PUBLIC_SERVER_URL;
  return NextResponse.json({
    baseUrl,
  });
}

Output for dokku config:show app_name

DOKKU_APP_RESTORE:       1
DOKKU_APP_TYPE:          dockerfile
DOKKU_DOCKERFILE_PORTS:  3000/tcp
DOKKU_PROXY_PORT_MAP:    http:3000:3000 http:80:3000 https:443:3000
DOKKU_PROXY_SSL_PORT:    443
NEXT_PUBLIC_SERVER_URL:  https://server_url.com

I have tried setting app specific environment variable using dokku config:set app_name NEXT_PUBLIC_SERVER_URL=https://server_url.com

Expect process.env.NEXT_PUBLIC_SERVER_URL to return https://server_url.com but returns nothing

Upvotes: 0

Views: 64

Answers (1)

Ozan Yurtseven
Ozan Yurtseven

Reputation: 830

Same always happen to me. If you're trying to access a variable that should only be used server-side, don't prefix it with NEXT_PUBLIC_. For example, if SERVER_URL is only needed in server-side functions (like API routes), set it without the prefix. But if you want it available both client-side and server-side, use the NEXT_PUBLIC_ prefix.

Set env variable like

dokku config:set app_name SERVER_URL=https://server_url.com

and use this server env variable

export async function GET() {
const baseUrl = process.env.SERVER_URL;
return NextResponse.json({
  baseUrl,
});

}

this should work because you want to reach from router. reference: Bundling Environment Variables for the Browser

Upvotes: 0

Related Questions