Handling basePath Dynamically in a Docker Deployment #75673
cfeltz-isia
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Goals
Be able to generate a docker image allowing to deploy a NextJs application working both with a pathPrefix (bastPath) and without.
Non-Goals
No response
Background
When deploying a Next.js application in a Docker container, the basePath and NEXT_PUBLIC_BASE_URL must be known at build time, which complicates deployments across multiple environments.
A common workaround is:
Defining constants at build time (basePath, NEXT_PUBLIC_BASE_URL, etc.).
Using an entrypoint.sh script with sed to replace these constants at runtime.
However, this approach has a major limitation:
If no basePath is set at build time, Next.js defaults to "/", which causes issues (routes not found, broken redirects, etc.).
This makes it impossible to create a single Docker image that works both with and without a basePath.
When basePath is not explicitly defined, Next.js treats it as "/", which is problematic because:
Routes are not resolved correctly.
Redirects do not work as expected.
It prevents an image from being reused in different environments dynamically.
The common workaround example with limitations:
.env.production
next.config.js
DockerFile
entrypoint.sh
Workaround
There is a workaround that consists of adding a compilation argument to the docker build command that specifies whether the generated image is made for deployment with or without a path prefix. But this requires compiling two separate images.
Proposal
Would it be possible for Next.js to:
This would significantly improve flexibility when deploying in Docker and avoid the need for environment-specific images.
Beta Was this translation helpful? Give feedback.
All reactions