Skip to content

NextJs application replace config does not use runtime variable instead uses build variables #5133

@vilasrajbhar

Description

@vilasrajbhar

Node.js Version

v22.21.1

NPM Version

v11.10.1

Operating System

apline

Subsystem

fs

Description

Issue: Runtime config not applied when starting container (Next.js + Sitecore JSS)

We are facing an issue where the runtime configuration values are not being picked up when the container starts. Even after replacing the config and then starting the Next.js application, it continues to use the build-time variables that were bundled during the next build process.

Expected Behavior:
We expected the app to:
Read environment variables when the container starts
Or dynamically pick up runtime config values
Reflect updated SITECORE_API_HOST and SITECORE_API_KEY without rebuilding

Actual Behavior

The app always uses the values that were present during node:server
Changing environment variables at container start has no effect
The bundled values remain hardcoded inside the compiled .next output
I am deployed .next folder which was generated at a time of build with local variables. When replacing in container with correct values does not have any impact.
This happens because:
Next.js replaces environment variables at build time and bundles them into the application.

What We Tried
1️⃣ Using .env
Defined SITECORE_API_HOST and SITECORE_API_KEY
Observed they are embedded during build
Changing them at container startup did not update the app

2️⃣ Moving config to config.json
We replaced .env usage with:
src/config/config.json

And loaded it inside:
pages/[[...path]].tsx

Using either:
import jssConfig from 'config.json'
or reading with fs.readFile

Result:
Still bundled at build time
Container runtime changes did not apply

Because:
Any imported JSON file is bundled during build
It becomes static just like .env

3️⃣ Attempted Runtime API (/api/runtime-config)
Created an API route to read config at runtime
Fetched it in _app.tsx
Worked on client side only
Did NOT affect server-side rendering
SSR still used build-time values

Root Cause
Next.js behavior:
process.env.* → resolved at build time
Imported JSON files → bundled at build time
Static imports are compiled into the app
Docker container runtime variables do not override bundled values

Minimal Reproduction

Output

No response

Before You Submit

  • I have looked for issues that already exist before submitting this
  • My issue follows the guidelines in the README file, and follows the 'How to ask a good question' guide at https://stackoverflow.com/help/how-to-ask

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions