Files
OpenFrontIO/index.html
scamiv 05e2bc9f0a Improve cacheability with content-hashed public assets and a cacheable app shell (#3494)
## Description:

This reworks asset delivery and cacheability across the app and moves
non-bundled public resources onto immutable, content-hashed URLs.

Vite bundle outputs continue to live under `/assets/**` and remain
content-hashed by Vite. Public resources that were previously fetched
from stable paths in `resources/` now go through a custom hashed
namespace under `/_assets/**`, backed by a generated asset manifest that
is available to the server, browser, and worker runtime.

In parallel, the root app shell is now cacheable shared HTML instead of
request-time `no-store` HTML. Dynamic and live routes remain explicitly
uncached.

## Why
- Improve browser and Cloudflare cacheability for static assets.
- Remove query-string and release-version cache busting for
runtime-fetched assets.
- Allow unchanged public assets to keep the same URL across releases.
- Reduce avoidable work on `/` by serving a shared app shell instead of
rendering HTML on every request.
- Make cache behavior explicit instead of relying on mixed framework
defaults and file-extension heuristics.

## What Changed

### 1. Content-hashed public asset pipeline
- Added a build-time public asset manifest and hashing pipeline for
non-Vite resources.
- Production now emits hashed public assets under `/_assets/**`.
- Added runtime manifest loading for Node so server-rendered paths
resolve against built hashed files instead of rebuilding from source at
runtime.
- Emitted the runtime asset manifest as an ESM module for server
consumption.

Result:
- `/assets/**` = Vite-managed hashed bundle outputs
- `/_assets/**` = custom content-hashed public resources

### 2. Runtime asset URL migration
- Added a shared `assetUrl(...)` resolution path.
- Migrated runtime references away from query-string versioning and
stable source paths.
- Updated browser, worker, and server-side rendering paths to resolve
through the asset manifest.
- Moved map manifests, map binaries, thumbnails, sprites, sounds, fonts,
flags, icons, screenshots, and other runtime-fetched resources onto
hashed URLs.

### 3. Map and preview fixes
- Fixed directory and per-file map asset resolution so map manifest and
binary fetches resolve to the correct hashed URLs.
- Updated preview metadata and map thumbnail paths to use the hashed
asset namespace.
- Fixed runtime manifest loading in prod after deployment.

### 4. Explicit cache policies
- Added explicit immutable cache headers for:
  - `/assets/**`
  - `/_assets/**`
  - worker-prefixed equivalents under `/wN/...`
- Added explicit `no-store` headers for live and dynamic APIs.
- Removed the old `/api/env` bootstrap request and baked `gameEnv` into
the HTML bootstrap instead.

### 5. Cacheable root app shell
- Refactored the root HTML path to serve a shared app shell with:
- `Cache-Control: public, max-age=0, s-maxage=300,
stale-while-revalidate=86400`
- `/` and the SPA fallback now serve shared cacheable HTML instead of
request-time `no-store` rendering.
- `/game/:id` remains dynamic and `no-store`, but now reuses the shared
shell before injecting preview tags.

### 6. Matchmaking instance handling
- Because the app shell is now cacheable, `INSTANCE_ID` was removed from
shared HTML.
- Added `/api/instance` as a temporary `no-store` runtime lookup used
only by matchmaking.
- This preserves correctness with the current random-per-boot
`INSTANCE_ID` model while keeping `/` cacheable, but it is not the
intended long-term design.

## Behavior Changes

### Asset URL contract
Production URLs for non-Vite public resources now change from stable
paths such as:
- `/maps/...`
- `/images/...`
- `/manifest.json`

to content-hashed paths under:
- `/_assets/...`

Examples:
- `/_assets/maps/<map>/manifest.<hash>.json`
- `/_assets/images/Favicon.<hash>.svg`

### Bootstrap/config
- `/api/env` is removed.
- `gameEnv` is now bootstrapped from HTML.

### HTML caching
- `/` and the SPA fallback are now cacheable shared HTML.
- `/game/:id` remains dynamic.

## Cache Matrix After This Branch
- `/_assets/**`: `public, max-age=31536000, immutable`
- `/assets/**`: `public, max-age=31536000, immutable`
- live `/api/**`: explicit `no-store`
- `/api/health`: explicit `no-store`
- `/api/instance`: explicit `no-store`
- `/game/:id`: explicit `no-store`
- `/` and SPA fallback: `public, max-age=0, s-maxage=300,
stale-while-revalidate=86400`

## Notes / Tradeoffs
- `/api/instance` is a temporary compromise. It exists because
`INSTANCE_ID` is currently random per boot, which is not safe to embed
into cacheable shared HTML.
- The current matchmaking flow still asks the client to provide
`instance_id` during `matchmaking/join`. That is functional, but it is
the wrong ownership boundary: instance selection should be handled by
the matchmaking service, not by the browser.
- The cleaner end-state would be:
- make `matchmaking/join` stop requiring `instance_id` from the client,
and let the matchmaking service select a healthy instance from worker
check-ins
- This branch makes the origin behavior edge-cache-friendly, but
Cloudflare still needs matching cache rules if HTML itself should be
cached at the edge.

## Validation
Verified during development with:
- `npx tsc --noEmit`
- `node node_modules\\vite\\bin\\vite.js build`
- `node node_modules\\vitest\\vitest.mjs run
tests/server/RenderHtml.test.ts tests/server/NoStoreHeaders.test.ts
tests/server/StaticAssetCache.test.ts
tests/core/configuration/ConfigLoader.test.ts`

Additional targeted tests added:
- `tests/AssetUrls.test.ts`
- `tests/core/game/FetchGameMapLoader.test.ts`
- `tests/core/configuration/ConfigLoader.test.ts`
- `tests/server/NoStoreHeaders.test.ts`
- `tests/server/StaticAssetCache.test.ts`
- `tests/server/RenderHtml.test.ts`

## Known Existing Warnings
The production build still reports pre-existing warnings that are not
addressed by this branch:
- inconsistent JSON import attributes for `resources/countries.json`
- inconsistent JSON import attributes for `resources/QuickChat.json`
- large chunk warnings from Vite

## Rollout Notes
- Cache rules should treat `/_assets/**` and `/assets/**` as immutable.
- Cloudflare will still classify HTML as dynamic after deploy unless
matching edge cache rules are configured for it.

## Follow-ups
- Remove `/api/instance` by changing `matchmaking/join` so the server
selects the target instance, or by making `INSTANCE_ID` deploy-stable if
the current contract must remain.


## Please complete the following:

- [ ] I have added screenshots for all UI updates
- [ ] I process any text displayed to the user through translateText()
and I've added it to the en.json file
- [ ] I have added relevant tests to the test directory
- [ ] I confirm I have thoroughly tested these changes and take full
responsibility for any bugs introduced

## Please put your Discord username so you can be contacted if a bug or
regression is found:

DISCORD_USERNAME
2026-03-23 11:36:52 -07:00

431 lines
14 KiB
HTML

<!doctype html>
<html lang="en" class="h-full preload" translate="no">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, viewport-fit=cover"
/>
<title data-i18n="main.title">OpenFront (ALPHA)</title>
<link rel="manifest" href="<%- manifestHref %>" />
<link rel="icon" type="image/svg+xml" href="<%- faviconHref %>" />
<!-- Preload styles -->
<style>
.preload {
visibility: hidden;
opacity: 0;
transition: opacity 0.5s ease-out;
}
/* iOS safe area support */
body {
padding-top: env(safe-area-inset-top);
padding-right: env(safe-area-inset-right);
padding-bottom: env(safe-area-inset-bottom);
padding-left: env(safe-area-inset-left);
}
/* Ensure full viewport height on iOS */
html,
body {
height: 100%;
height: -webkit-fill-available;
min-height: 100%;
min-height: -webkit-fill-available;
}
</style>
<!-- SEO -->
<link rel="canonical" href="https://openfront.io/" />
<meta
name="description"
content="Conquer the world in this multiplayer battle royale! Expand your nation, eliminate opponents, and dominate the map in this fast-paced IO game."
/>
<!-- Open Graph -->
<meta property="og:url" content="https://openfront.io/" />
<meta property="og:title" content="OpenFront - Battle Royale" />
<meta
property="og:description"
content="Conquer the world in this multiplayer battle royale! Expand your nation, eliminate opponents, and dominate the map in this fast-paced IO game."
/>
<meta property="og:image" content="<%- gameplayScreenshotUrl %>" />
<meta property="og:type" content="game" />
<!-- Injected from Server env -->
<script>
window.GIT_COMMIT = <%- gitCommit %>;
window.ASSET_MANIFEST = <%- assetManifest %>;
window.BOOTSTRAP_CONFIG = {
gameEnv: <%- gameEnv %>,
};
document.documentElement.style.setProperty(
"--background-image-url",
`url("<%- backgroundImageUrl %>")`,
);
document.documentElement.style.setProperty(
"--desktop-logo-image-url",
`url("<%- desktopLogoImageUrl %>")`,
);
document.documentElement.style.setProperty(
"--mobile-logo-image-url",
`url("<%- mobileLogoImageUrl %>")`,
);
</script>
<!-- CrazyGames SDK -->
<script
src="https://sdk.crazygames.com/crazygames-sdk-v3.js"
async
></script>
<!-- Cloudflare Turnstile -->
<script
src="https://challenges.cloudflare.com/turnstile/v0/api.js"
async
defer
></script>
<script data-cfasync="false">
window.ramp = window.ramp || {};
window.ramp.que = window.ramp.que || [];
window.ramp.passiveMode = true;
</script>
<script>
window.googletag = window.googletag || { cmd: [] };
googletag.cmd.push(function () {
googletag.pubads().set("page_url", "http://openfront.io ");
});
</script>
<!-- Analytics -->
<script
async
src="https://www.googletagmanager.com/gtag/js?id=AW-16702609763"
></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag("js", new Date());
gtag("config", "AW-16702609763");
</script>
<!-- Google tag (gtag.js) -->
<script
async
src="https://www.googletagmanager.com/gtag/js?id=G-WQGQQ8RDN4"
></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag("js", new Date());
gtag("config", "G-WQGQQ8RDN4");
</script>
</head>
<body
class="h-full select-none font-sans min-h-screen bg-neutral-800 transition-opacity duration-300 ease-in-out flex flex-row overflow-hidden"
>
<div id="hex-grid" class="fixed inset-0 -z-50 pointer-events-none">
<div
id="background-layer"
class="absolute inset-0 bg-cover bg-center opacity-30 [filter:brightness(1.0)] dark:[filter:sepia(0.2)_saturate(1.2)_hue-rotate(180deg)_brightness(0.9)]"
style="background-image: var(--background-image-url)"
></div>
<div
class="absolute inset-0 bg-center bg-no-repeat bg-contain hidden lg:block"
style="background-image: var(--desktop-logo-image-url); opacity: 0.5"
></div>
<div
class="absolute inset-0 bg-center bg-no-repeat bg-contain lg:hidden"
style="background-image: var(--mobile-logo-image-url); opacity: 0.5"
></div>
</div>
<!-- LEFT SIDEBAR MENU -->
<div
id="mobile-menu-backdrop"
class="lg:hidden! in-[.in-game]:hidden hidden pointer-events-none [&.open]:block [&.open]:pointer-events-auto [&.open]:fixed [&.open]:inset-0 [&.open]:bg-black/60 [&.open]:z-[40000] transition-opacity"
role="presentation"
aria-hidden="true"
></div>
<mobile-nav-bar
id="sidebar-menu"
class="peer [.in-game_&]:hidden z-[40001] fixed left-0 top-0 h-full flex flex-col justify-start overflow-visible bg-black/70 backdrop-blur-xl border-r border-white/10 transition-transform duration-500 ease-out transform -translate-x-full w-[70%] [&.open]:translate-x-0 lg:hidden"
role="dialog"
data-i18n-aria-label="main.menu"
></mobile-nav-bar>
<!-- MAIN CONTENT AREA -->
<div
class="in-[.in-game]:hidden flex-1 relative overflow-hidden h-full transition-[margin] duration-500 ease-out will-change-[margin-left] flex flex-col"
>
<!-- Desktop Top Bar -->
<desktop-nav-bar></desktop-nav-bar>
<div
id="turnstile-container"
class="fixed top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 z-99999"
></div>
<gutter-ads></gutter-ads>
<!-- Main container with responsive padding -->
<main-layout class="contents">
<play-page class="contents"></play-page>
<matchmaking-modal
id="page-matchmaking"
inline
class="hidden w-full h-full page-content relative z-50"
></matchmaking-modal>
<news-modal
id="page-news"
inline
class="hidden w-full h-full page-content relative z-50"
></news-modal>
<single-player-modal
id="page-single-player"
inline
class="hidden w-full h-full page-content relative z-50"
></single-player-modal>
<host-lobby-modal
id="page-host-lobby"
inline
class="hidden w-full h-full page-content relative z-50"
></host-lobby-modal>
<join-lobby-modal
id="page-join-lobby"
inline
class="hidden w-full h-full page-content relative z-50"
></join-lobby-modal>
<territory-patterns-modal
id="page-item-store"
inline
class="hidden w-full h-full page-content relative z-50"
></territory-patterns-modal>
<user-setting
id="page-settings"
inline
class="hidden w-full h-full page-content relative z-50"
></user-setting>
<leaderboard-modal
id="page-leaderboard"
inline
class="hidden w-full h-full page-content relative z-50"
></leaderboard-modal>
<troubleshooting-modal
id="page-troubleshooting"
inline
class="hidden w-full h-full page-content relative z-50"
></troubleshooting-modal>
<account-modal
id="page-account"
inline
class="hidden w-full h-full page-content relative z-50"
></account-modal>
<help-modal
id="page-help"
inline
class="hidden w-full h-full page-content relative z-50"
></help-modal>
<language-modal
id="page-language"
inline
class="hidden w-full h-full page-content relative z-50"
></language-modal>
<flag-input-modal
id="flag-input-modal"
inline
class="hidden w-full h-full page-content relative z-50"
></flag-input-modal>
<ranked-modal
id="page-ranked"
inline
class="hidden w-full h-full page-content relative z-50"
></ranked-modal>
</main-layout>
<!-- Ad above footer -->
<div class="[.in-game_&]:hidden mt-auto flex flex-col shrink-0">
<home-footer-ad></home-footer-ad>
<page-footer></page-footer>
</div>
<!-- Global Modals -->
<territory-patterns-modal
id="territory-patterns-modal"
></territory-patterns-modal>
</div>
<!-- Game components -->
<div id="app"></div>
<!-- Bottom HUD: <sm=column, sm..lg=2col (HUD left | events right), lg+=3col grid centered -->
<div
class="fixed bottom-0 left-0 w-full z-[200] flex flex-col pointer-events-none sm:flex-row sm:items-end lg:grid lg:grid-cols-[1fr_500px_1fr] lg:items-end min-[1200px]:px-4"
style="
padding-bottom: env(safe-area-inset-bottom);
padding-left: env(safe-area-inset-left);
padding-right: env(safe-area-inset-right);
"
>
<!-- HUD: <sm contents (children join outer flex), sm+ flex-col 460px, lg+ col-2 -->
<div
class="contents sm:flex sm:flex-col sm:pointer-events-none w-full sm:w-[500px] lg:col-start-2 sm:z-10"
>
<attacks-display
class="w-full pointer-events-auto order-1 sm:order-none"
></attacks-display>
<div
class="pointer-events-auto bg-gray-800/92 backdrop-blur-sm sm:rounded-tr-lg lg:rounded-t-lg min-[1200px]:rounded-lg shadow-lg order-3 sm:order-none"
>
<control-panel class="w-full"></control-panel>
<unit-display class="hidden lg:block w-full"></unit-display>
</div>
</div>
<!-- events+chat: <sm between attacks and control (order-2), sm+ right side, lg+ col-3 -->
<div
class="flex flex-col pointer-events-none items-end order-2 sm:order-none sm:flex-1 lg:col-start-3 lg:self-end lg:justify-end min-[1200px]:mr-4"
>
<chat-display
class="w-full sm:w-auto pointer-events-auto"
></chat-display>
<events-display
class="w-full sm:w-auto pointer-events-auto"
></events-display>
</div>
</div>
<!-- Game modals and overlays -->
<emoji-table></emoji-table>
<build-menu></build-menu>
<win-modal></win-modal>
<game-starting-modal></game-starting-modal>
<div
class="flex flex-col items-end fixed top-0 right-0 min-[1200px]:top-4 min-[1200px]:right-4 z-1000 gap-2"
>
<game-right-sidebar></game-right-sidebar>
<replay-panel></replay-panel>
</div>
<settings-modal></settings-modal>
<player-panel></player-panel>
<spawn-timer></spawn-timer>
<immunity-timer></immunity-timer>
<in-game-promo></in-game-promo>
<game-info-modal></game-info-modal>
<alert-frame></alert-frame>
<chat-modal></chat-modal>
<multi-tab-modal></multi-tab-modal>
<game-left-sidebar></game-left-sidebar>
<performance-overlay></performance-overlay>
<player-info-overlay></player-info-overlay>
<leader-board></leader-board>
<team-stats></team-stats>
<heads-up-message></heads-up-message>
<!-- Scripts -->
<script>
// Remove preload class after everything is loaded
window.addEventListener("load", function () {
requestAnimationFrame(() => {
document.documentElement.classList.remove("preload");
});
});
// Fallback: remove preload class after timeout in case DOMContentLoaded never fires
setTimeout(function () {
document.documentElement.classList.remove("preload");
}, 3000);
</script>
<script>
// Fallback sidebar toggle so hamburger works even if module bundle fails to load
window.__toggleSidebar = function (e) {
try {
const sidebar = document.getElementById("sidebar-menu");
const backdrop = document.getElementById("mobile-menu-backdrop");
if (!sidebar || !backdrop) return;
const isOpen = sidebar.classList.contains("open");
if (isOpen) {
sidebar.classList.remove("open");
backdrop.classList.remove("open");
document.documentElement.classList.remove("overflow-hidden");
sidebar.setAttribute("aria-hidden", "true");
sidebar.removeAttribute("aria-modal");
backdrop.setAttribute("aria-hidden", "true");
} else {
sidebar.classList.add("open");
backdrop.classList.add("open");
document.documentElement.classList.add("overflow-hidden");
sidebar.setAttribute("aria-hidden", "false");
sidebar.setAttribute("aria-modal", "true");
backdrop.setAttribute("aria-hidden", "false");
}
const hb = document.getElementById("hamburger-btn");
if (hb) hb.setAttribute("aria-expanded", (!isOpen).toString());
} catch (err) {
console.error("Toggle failed", err);
}
};
// Wire up the hamburger button inline
const hamburger = document.getElementById("hamburger-btn");
if (hamburger) {
hamburger.onclick = window.__toggleSidebar;
hamburger.setAttribute("aria-expanded", "false");
}
// Wire up backdrop click to close menu
const backdrop = document.getElementById("mobile-menu-backdrop");
if (backdrop) {
backdrop.onclick = function (e) {
const sidebar = document.getElementById("sidebar-menu");
if (sidebar && sidebar.classList.contains("open")) {
window.__toggleSidebar(e);
}
};
}
// Wire up Escape key to close menu
document.addEventListener("keydown", function (e) {
if (e.key === "Escape" || e.key === "Esc") {
const sidebar = document.getElementById("sidebar-menu");
if (sidebar && sidebar.classList.contains("open")) {
window.__toggleSidebar(e);
}
}
});
</script>
<!-- Analytics -->
<script
defer
src="https://static.cloudflareinsights.com/beacon.min.js"
data-cf-beacon='{"token": "03d93e6fefb349c28ee69b408fa25a13"}'
></script>
<script type="module" src="/src/client/Main.ts"></script>
<footer>
<script
data-cfasync="false"
async
src="//cdn.intergient.com/1025558/75940/ramp.js"
></script>
</footer>
</body>
</html>