mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-21 12:10:46 +00:00
Bake server env into HTML bootstrap
This commit is contained in:
@@ -58,6 +58,9 @@
|
||||
window.GIT_COMMIT = <%- gitCommit %>;
|
||||
window.INSTANCE_ID = <%- instanceId %>;
|
||||
window.ASSET_MANIFEST = <%- assetManifest %>;
|
||||
window.BOOTSTRAP_CONFIG = {
|
||||
gameEnv: <%- gameEnv %>,
|
||||
};
|
||||
document.documentElement.style.setProperty(
|
||||
"--background-image-url",
|
||||
`url("<%- backgroundImageUrl %>")`,
|
||||
|
||||
@@ -9,6 +9,14 @@ 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,
|
||||
@@ -30,16 +38,20 @@ export async function getServerConfigFromClient(): Promise<ServerConfig> {
|
||||
if (cachedSC) {
|
||||
return cachedSC;
|
||||
}
|
||||
const response = await fetch("/api/env");
|
||||
|
||||
const bootstrapGameEnv = window.BOOTSTRAP_CONFIG?.gameEnv;
|
||||
if (bootstrapGameEnv) {
|
||||
cachedSC = getServerConfig(bootstrapGameEnv);
|
||||
return cachedSC;
|
||||
}
|
||||
|
||||
const response = await fetch("/api/env");
|
||||
if (!response.ok) {
|
||||
throw new Error(
|
||||
`Failed to fetch server config: ${response.status} ${response.statusText}`,
|
||||
);
|
||||
}
|
||||
const config = await response.json();
|
||||
// Log the retrieved configuration
|
||||
console.log("Server config loaded:", config);
|
||||
|
||||
cachedSC = getServerConfig(config.game_env);
|
||||
return cachedSC;
|
||||
@@ -63,3 +75,7 @@ export function getServerConfig(gameEnv: string) {
|
||||
throw Error(`unsupported server configuration: ${gameEnv}`);
|
||||
}
|
||||
}
|
||||
|
||||
export function clearCachedServerConfig(): void {
|
||||
cachedSC = null;
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ export async function renderHtmlContent(htmlPath: string): Promise<string> {
|
||||
gitCommit: JSON.stringify(process.env.GIT_COMMIT ?? "undefined"),
|
||||
instanceId: JSON.stringify(process.env.INSTANCE_ID ?? "undefined"),
|
||||
assetManifest: JSON.stringify(assetManifest),
|
||||
gameEnv: JSON.stringify(process.env.GAME_ENV ?? "dev"),
|
||||
manifestHref: buildAssetUrl("manifest.json", assetManifest),
|
||||
faviconHref: buildAssetUrl("images/Favicon.svg", assetManifest),
|
||||
gameplayScreenshotUrl: buildAssetUrl(
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
import { beforeEach, describe, expect, test, vi } from "vitest";
|
||||
import { GameEnv } from "../../../src/core/configuration/Config";
|
||||
import {
|
||||
clearCachedServerConfig,
|
||||
getServerConfigFromClient,
|
||||
} from "../../../src/core/configuration/ConfigLoader";
|
||||
|
||||
describe("ConfigLoader", () => {
|
||||
beforeEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
window.BOOTSTRAP_CONFIG = undefined;
|
||||
clearCachedServerConfig();
|
||||
});
|
||||
|
||||
test("uses bootstrap config without fetching /api/env", async () => {
|
||||
window.BOOTSTRAP_CONFIG = { gameEnv: "prod" };
|
||||
const fetchSpy = vi.spyOn(globalThis, "fetch");
|
||||
|
||||
const config = await getServerConfigFromClient();
|
||||
|
||||
expect(config.env()).toBe(GameEnv.Prod);
|
||||
expect(fetchSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -27,6 +27,7 @@ export default defineConfig(({ mode }) => {
|
||||
: {};
|
||||
const htmlAssetData = {
|
||||
assetManifest: JSON.stringify(assetManifest),
|
||||
gameEnv: JSON.stringify(env.GAME_ENV ?? "dev"),
|
||||
manifestHref: buildAssetUrl("manifest.json", assetManifest),
|
||||
faviconHref: buildAssetUrl("images/Favicon.svg", assetManifest),
|
||||
gameplayScreenshotUrl: buildAssetUrl(
|
||||
|
||||
Reference in New Issue
Block a user