From afd6e3ecaa1f5abe4c9f677fe1cc16bd5ae12e4f Mon Sep 17 00:00:00 2001 From: scamiv <6170744+scamiv@users.noreply.github.com> Date: Tue, 24 Mar 2026 14:12:20 +0100 Subject: [PATCH] Make client config loading worker-safe --- src/core/configuration/ConfigLoader.ts | 14 ++++++++++---- tests/core/configuration/ConfigLoader.test.ts | 11 +++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/core/configuration/ConfigLoader.ts b/src/core/configuration/ConfigLoader.ts index 4987da1c3..fd8585230 100644 --- a/src/core/configuration/ConfigLoader.ts +++ b/src/core/configuration/ConfigLoader.ts @@ -39,12 +39,18 @@ export async function getServerConfigFromClient(): Promise { return cachedSC; } - const bootstrapGameEnv = window.BOOTSTRAP_CONFIG?.gameEnv; - if (!bootstrapGameEnv) { - throw new Error("Missing bootstrap server config"); + const bootstrapGameEnv = + typeof window !== "undefined" + ? window.BOOTSTRAP_CONFIG?.gameEnv + : undefined; + const bundledGameEnv = + typeof process !== "undefined" ? process.env?.GAME_ENV : undefined; + const gameEnv = bootstrapGameEnv ?? bundledGameEnv; + if (!gameEnv) { + throw new Error("Missing client server config"); } - cachedSC = getServerConfig(bootstrapGameEnv); + cachedSC = getServerConfig(gameEnv); return cachedSC; } export function getServerConfigFromServer(): ServerConfig { diff --git a/tests/core/configuration/ConfigLoader.test.ts b/tests/core/configuration/ConfigLoader.test.ts index 65fd81757..105f889ee 100644 --- a/tests/core/configuration/ConfigLoader.test.ts +++ b/tests/core/configuration/ConfigLoader.test.ts @@ -6,9 +6,12 @@ import { } from "../../../src/core/configuration/ConfigLoader"; describe("ConfigLoader", () => { + const originalGameEnv = process.env.GAME_ENV; + beforeEach(() => { vi.restoreAllMocks(); window.BOOTSTRAP_CONFIG = undefined; + process.env.GAME_ENV = originalGameEnv; clearCachedServerConfig(); }); @@ -21,4 +24,12 @@ describe("ConfigLoader", () => { expect(config.env()).toBe(GameEnv.Prod); expect(fetchSpy).not.toHaveBeenCalled(); }); + + test("falls back to bundled env when bootstrap config is unavailable", async () => { + process.env.GAME_ENV = "prod"; + + const config = await getServerConfigFromClient(); + + expect(config.env()).toBe(GameEnv.Prod); + }); });