Access runtime env vars from next.config.js

Daniel Porteous

I have the following in my next.config.js:

/** @type {import('next').NextConfig} */
const nextConfig = {
  output: "standalone",
  // If set, use the given URL prefix for fetching static content instead of serving
  // it directly from the next app.
  assetPrefix: process.env.NEXT_PUBLIC_CDN_PREFIX,
};

The focus here is assetPrefix. I want this value to be read at runtime to inform the client where to fetch static assets. The issue is, even though I have this env var set, it doesn't work because I believe the final value for assetPrefix is determined at build time, but the env var is only set during run time.

For the rest of my app I'm solving this by using next-runtime-env but I can't seem to get that to work in next.config.js.

Is there a way to set assetPrefix based on an env var at runtime?

I'm running NextJS from a Docker image. I build the app as part of building the image, which I'd prefer to keep doing rather than building as part of the command when I run a container from that image. I suspect you could solve this with middleware instead of assetPrefix but I'd rather use what NextJS provides out of the box.

I'm using NextJS version 14.0.2 and next-runtime-env 3.0.1.

Daniel Porteous

While accessing runtime env vars is not possible for assetPrefix in next.config.js, you can achieve the same goal by instead using middleware. To do this, create a file called middleware.ts in the root of your app (e.g. src/), with content like this:

import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";

// If NEXT_PUBLIC_CDN_PREFIX is set, redirect requests for static content to the CDN.
export function middleware(request: NextRequest) {
  // If the env var is not set, just forward the request to the next middleware.
  if (process.env.NEXT_PUBLIC_CDN_PREFIX === undefined) {
    return NextResponse.next();
  }

  // Build a new URL with the CDN prefix as the hostname.
  const { nextUrl } = request;
  const { pathname } = nextUrl;
  const destinationURL = `${process.env.NEXT_PUBLIC_CDN_PREFIX}${pathname}`;

  return NextResponse.redirect(destinationURL);
}

// Run the middleware for all requests to the static content path.
export const config = {
  matcher: "/_next/static/(.*)",
};

See more about this topic here:

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Dart 2 AngularDart how to config env vars in deployment environment?

'env -i' -- doesn't remove all env vars from subshell

M4: Is it possible to access bash env vars from within m4 script?

How to generate a file with env vars in a job in Circleci config.yml

Load ENV vars with dotenv before node-config

Can I use env.js file instead of .env file to load environment variables using next.js in next.config.js file?

How do I get access to a token in .env to fetch data in Next.js?

Impossible to access Heroku config vars from python code

How to update Env vars from jenkins Powershell script

IntelliJ: setting AWS env vars for gradle run config

Access Environment Variables from SASS in Next.JS

Setting ENV vars from host to guest in Vagrant

Reading secret config file on Heroku without using env vars

cant access to .vars.prototype from macros

Node.js express: access application config from required modules

passing vars from js to node

Env Vars is undefined in my seed.js file (NODEJS)

Heroku config vars/env vars not working (using node.js)

How to use .env value in nuxt.config.js with runtime config

Is it possible to access localstorage from Angular proxy.config.js file?

next.config.js images domains problem with env

Nuxt 3 Runtime config values are not replaced by env variables

How to configure a spfx extension at runtime like ENV vars?

React.JS / Next.JS NODE_ENV=production runtime error

can next js api endpoint be access from other app

Next.js Fonts and CSS vars

Maven and env vars from file

In Next.js, unable to access the .env file using process.env.CLIENT_ID during the initial loading of Next.js

Karate framework's special tags @env/@envnot do not consider env value from karate-config.js