Inject MAP_TILE_URL into window.__ENV__ via html.tags + Docker build-arg
CI / ci (push) Failing after 32s
Deploy / build-and-deploy (push) Failing after 6s

http://flights-ui.devwebzavod.ru/ru/flights-map was still hitting the
same-origin tile path after adding the k8s env: Modern.js renders the
<Suspense> fallback on the server (i18n isn't preloaded), so the route
component that reads getEnv() never actually runs during SSR. The page
hydrates client-side, where process.env is Rspack's empty stub and
MAP_TILE_URL is never set — getEnv() falls back to the default.

Move the value into window.__ENV__ instead:

- modern.config.ts: inline a <script> into html.tags that sets
  window.__ENV__ = { MAP_TILE_URL: <value> } at SSR-server startup.
  The snippet is authored once at server boot, so the HTML template
  baked into dist/standalone/html/main/index.html always carries the
  pod's tile URL.
- src/env/index.ts: merge window.__ENV__ on top of process.env so the
  browser prefers the injected value (process.env only has NODE_ENV
  after Rspack's polyfill).
- Dockerfile.react: accept MAP_TILE_URL as a build ARG and expose it
  as ENV before `pnpm build:standalone`, so Modern.js picks it up when
  building the HTML template. k8s env still flows into the Node SSR
  process; the build-arg path guarantees correctness even when the
  runtime env is stripped.
- deployment/build-docker.sh: forward MAP_TILE_URL through as a
  build-arg (default keeps the same-origin path). CI on the
  devwebzavod cluster can export MAP_TILE_URL=https://flights.test.aeroflot.ru/map/api/tile/{z}/{x}/{y}.jpeg
  before running build-docker.sh and the resulting image will serve
  tiles from the upstream the real Aeroflot ingress terminates.
This commit is contained in:
2026-04-18 23:26:56 +03:00
parent 496a72e7d7
commit ef85ae6ea1
6 changed files with 170 additions and 9 deletions
+17 -9
View File
@@ -59,15 +59,23 @@ let cached: Env | undefined;
export function getEnv(): Env {
if (cached) return cached;
// In the browser, process.env is not available (Rspack doesn't replace
// bracket-notation access). Fall back to an SSR-injected window.__ENV__
// or sensible development defaults.
const envSource =
typeof process !== "undefined" && process.env
? process.env
: typeof window !== "undefined" && (window as unknown as Record<string, unknown>)["__ENV__"]
? (window as unknown as Record<string, unknown>)["__ENV__"] as Record<string, string>
: {};
// In Node (SSR build) process.env is authoritative. In the browser,
// Rspack injects a stub `process.env` that only carries NODE_ENV, so we
// can't rely on it for per-deployment values — prefer `window.__ENV__`
// (populated at SSR server start by modern.config.ts) and fall through
// to the stub only for NODE_ENV / defaults.
const isBrowser = typeof window !== "undefined";
const windowEnv = isBrowser
? ((window as unknown as Record<string, unknown>)["__ENV__"] as
| Record<string, string>
| undefined)
: undefined;
const processEnv =
typeof process !== "undefined" && process.env ? process.env : undefined;
const envSource: Record<string, string | undefined> = {
...(processEnv ?? {}),
...(windowEnv ?? {}),
};
const parsed = EnvSchema.safeParse(envSource);
if (!parsed.success) {