Use bundled GAME_ENV for client config

This commit is contained in:
scamiv
2026-03-24 14:42:25 +01:00
parent 496f1008bb
commit 73a2726d5b
2 changed files with 10 additions and 15 deletions
+5 -12
View File
@@ -9,14 +9,6 @@ import { prodConfig } from "./ProdConfig";
export let cachedSC: ServerConfig | null = null;
declare global {
interface Window {
BOOTSTRAP_CONFIG?: {
gameEnv?: string;
};
}
}
export async function getConfig(
gameConfig: GameConfig,
userSettings: UserSettings | null,
@@ -39,12 +31,13 @@ export async function getServerConfigFromClient(): Promise<ServerConfig> {
return cachedSC;
}
const bootstrapGameEnv = window.BOOTSTRAP_CONFIG?.gameEnv;
if (!bootstrapGameEnv) {
throw new Error("Missing bootstrap server config");
// Vite replaces this in browser and worker bundles at build time.
const gameEnv = process.env.GAME_ENV;
if (!gameEnv) {
throw new Error("Missing client server config");
}
cachedSC = getServerConfig(bootstrapGameEnv);
cachedSC = getServerConfig(gameEnv);
return cachedSC;
}
export function getServerConfigFromServer(): ServerConfig {
@@ -6,14 +6,16 @@ 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();
});
test("uses bootstrap config without fetching /api/env", async () => {
window.BOOTSTRAP_CONFIG = { gameEnv: "prod" };
test("uses bundled GAME_ENV without fetching /api/env", async () => {
process.env.GAME_ENV = "prod";
const fetchSpy = vi.spyOn(globalThis, "fetch");
const config = await getServerConfigFromClient();