Replies: 2 comments
|
Vite cannot make |
|
The standard and most reliable pattern for "build once, deploy anywhere" with a Vite SPA on Nginx without rebuilding is using an injected runtime config file generated at container startup ( Recommended Pattern:
<script src="/env-config.js"></script>
#!/bin/sh
cat <<EOF > /usr/share/nginx/html/env-config.js
window.__ENV__ = {
API_URL: "${API_URL:-http://localhost:8000}",
ENVIRONMENT: "${ENVIRONMENT:-production}"
};
EOF
exec nginx -g 'daemon off;'
declare global {
interface Window {
__ENV__?: Record<string, string>;
}
}
export const config = {
apiUrl: window.__ENV__?.API_URL || import.meta.env.VITE_API_URL || "http://localhost:8000",
environment: window.__ENV__?.ENVIRONMENT || import.meta.env.MODE,
};This keeps your Vite production artifacts completely immutable across Dev/Staging/Prod Docker deployments. |
Uh oh!
There was an error while loading. Please reload this page.
Hey folks. I'm moving a Vite SPA to a strict "build once, deploy anywhere" Docker pipeline.
The problem is import.meta.env is statically replaced during vite build. If I build the Docker image in my CI pipeline, the staging variables get baked into the minified bundle. I need to swap API URLs at runtime based on the container environment.
Since this is a pure SPA being served by Nginx (no Node SSR to inject them dynamically), how are you guys cleanly handling runtime configuration in Vite without triggering rebuilds?
All reactions