mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-21 11:40:42 +00:00
Load asset manifest from built static files at runtime
This commit is contained in:
@@ -3,10 +3,7 @@ import { buildAssetUrl } from "../core/AssetUrls";
|
||||
import { ClanTagSchema, GameInfo, UsernameSchema } from "../core/Schemas";
|
||||
import { formatPlayerDisplayName } from "../core/Util";
|
||||
import { GameMode } from "../core/game/Game";
|
||||
import {
|
||||
buildPublicAssetManifest,
|
||||
getResourcesDir,
|
||||
} from "./PublicAssetManifest";
|
||||
import { getRuntimeAssetManifest } from "./RuntimeAssetManifest";
|
||||
|
||||
export const PlayerInfoSchema = z.object({
|
||||
clientID: z.string().optional(),
|
||||
@@ -136,17 +133,14 @@ export function escapeHtml(value: string): string {
|
||||
.replace(/'/g, "'");
|
||||
}
|
||||
|
||||
export function buildPreview(
|
||||
export async function buildPreview(
|
||||
gameID: string,
|
||||
origin: string,
|
||||
workerPath: string,
|
||||
lobby: GameInfo | null,
|
||||
publicInfo: ExternalGameInfo | null,
|
||||
): PreviewMeta {
|
||||
const assetManifest =
|
||||
process.env.GAME_ENV === "prod"
|
||||
? buildPublicAssetManifest(getResourcesDir())
|
||||
: {};
|
||||
): Promise<PreviewMeta> {
|
||||
const assetManifest = await getRuntimeAssetManifest();
|
||||
const buildAbsoluteAssetUrl = (path: string) =>
|
||||
new URL(buildAssetUrl(path, assetManifest), origin).toString();
|
||||
const isFinished = !!publicInfo?.info?.end;
|
||||
|
||||
@@ -96,7 +96,7 @@ export function registerGamePreviewRoute(opts: {
|
||||
}
|
||||
|
||||
const origin = requestOrigin(req, config);
|
||||
const meta = buildPreview(
|
||||
const meta = await buildPreview(
|
||||
gameID,
|
||||
origin,
|
||||
config.workerPath(gameID),
|
||||
|
||||
@@ -2,17 +2,11 @@ import ejs from "ejs";
|
||||
import type { Response } from "express";
|
||||
import fs from "fs/promises";
|
||||
import { buildAssetUrl } from "../core/AssetUrls";
|
||||
import {
|
||||
buildPublicAssetManifest,
|
||||
getResourcesDir,
|
||||
} from "./PublicAssetManifest";
|
||||
import { getRuntimeAssetManifest } from "./RuntimeAssetManifest";
|
||||
|
||||
export async function renderHtmlContent(htmlPath: string): Promise<string> {
|
||||
const htmlContent = await fs.readFile(htmlPath, "utf-8");
|
||||
const assetManifest =
|
||||
process.env.GAME_ENV === "prod"
|
||||
? buildPublicAssetManifest(getResourcesDir())
|
||||
: {};
|
||||
const assetManifest = await getRuntimeAssetManifest();
|
||||
return ejs.render(htmlContent, {
|
||||
gitCommit: JSON.stringify(process.env.GIT_COMMIT ?? "undefined"),
|
||||
instanceId: JSON.stringify(process.env.INSTANCE_ID ?? "undefined"),
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
import fs from "fs/promises";
|
||||
import path from "path";
|
||||
import { fileURLToPath } from "url";
|
||||
import type { AssetManifest } from "../core/AssetUrls";
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
const staticDir = path.join(__dirname, "../../static");
|
||||
const manifestPath = path.join(staticDir, "_assets", "asset-manifest.json");
|
||||
|
||||
let manifestPromise: Promise<AssetManifest> | null = null;
|
||||
|
||||
async function readRuntimeAssetManifest(): Promise<AssetManifest> {
|
||||
const raw = await fs.readFile(manifestPath, "utf8");
|
||||
return JSON.parse(raw) as AssetManifest;
|
||||
}
|
||||
|
||||
export async function getRuntimeAssetManifest(): Promise<AssetManifest> {
|
||||
if (process.env.GAME_ENV !== "prod") {
|
||||
return {};
|
||||
}
|
||||
|
||||
manifestPromise ??= readRuntimeAssetManifest();
|
||||
return manifestPromise;
|
||||
}
|
||||
|
||||
export function clearRuntimeAssetManifestCache(): void {
|
||||
manifestPromise = null;
|
||||
}
|
||||
Reference in New Issue
Block a user