Files
galaxy-game/ui/frontend/src/service-worker.ts
T
Ilia Denisov 8565942392
Build · Site / build (push) Successful in 8s
Tests · Go / test (push) Successful in 2m22s
Tests · UI / test (push) Failing after 2m42s
feat(deploy): single-origin path-based deployment + project site
Serve the whole stack behind one host: site at /, game UI at /game/,
gateway REST at /api + /healthz, Connect at /rpc (prefix stripped by the
edge Caddy). The built artifact is domain-agnostic — the UI talks to the
gateway same-origin via relative URLs, so the same bundle runs under any
host with no rebuild and with CORS disabled.

- Rename the Connect proto service galaxy.gateway.v1.EdgeGateway ->
  edge.v1.Gateway; regenerate Go + TS; public path /rpc/edge.v1.Gateway.
- Move the game UI under base path /game (env BASE_PATH); make the
  manifest, service-worker scope, WASM loader, and all navigation
  base-aware via a withBase helper.
- Relative API + /rpc Connect prefix; Vite dev proxy mirrors the strip.
- Rewrite the edge Caddy (dev + prod) for path-based routing; empty CORS
  allow-lists (same-origin); single host.
- New VitePress project site (site/): i18n en/ru with switcher, LaTeX
  math, minimal monospace theme; built and served at /.
- dev-deploy compose/Makefile + CI (dev-deploy, prod-build, new
  site-build) build and seed the site; probes hit /, /game/, /healthz.
- Sync docs (ARCHITECTURE, gateway README/openapi, dev-deploy &
  local-dev READMEs, CLAUDE.md, ui/PLAN).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-23 18:19:07 +02:00

78 lines
2.5 KiB
TypeScript

/// <reference types="@sveltejs/kit" />
/// <reference lib="webworker" />
// Native SvelteKit service worker (no Workbox). It precaches the app
// shell, the build artefacts (JS/CSS + core.wasm), and the static files
// under a version-keyed cache, so the installed PWA loads offline and a
// new deploy (new `version`) drops the stale cache instead of serving
// old code. The gateway is never intercepted — it is always live network.
//
// SvelteKit registers this worker automatically in the production build.
import { base, build, files, version } from "$service-worker";
const sw = self as unknown as ServiceWorkerGlobalScope;
const CACHE = `galaxy-cache-${version}`;
// `${base}/` is the SPA shell (adapter-static fallback); precaching it
// makes the start_url load offline. `base` is empty at the root and
// `/game` under the single-origin deployment, and `$service-worker`
// derives it from `location.pathname` so it stays correct in a subdir.
const PRECACHE = [`${base}/`, ...build, ...files];
sw.addEventListener("install", (event) => {
event.waitUntil(
caches
.open(CACHE)
.then((cache) => cache.addAll(PRECACHE))
.then(() => sw.skipWaiting()),
);
});
sw.addEventListener("activate", (event) => {
event.waitUntil(
(async () => {
for (const key of await caches.keys()) {
if (key !== CACHE) await caches.delete(key);
}
await sw.clients.claim();
})(),
);
});
sw.addEventListener("fetch", (event) => {
const { request } = event;
if (request.method !== "GET") return;
const url = new URL(request.url);
// Cross-origin (the gateway) is always live network — never cached.
if (url.origin !== sw.location.origin) return;
event.respondWith(
(async () => {
const cache = await caches.open(CACHE);
// Version-keyed build/files: cache-first (content-hashed/immutable).
if (PRECACHE.includes(url.pathname)) {
const hit = await cache.match(url.pathname);
if (hit) return hit;
}
// Everything else: network-first, falling back to the cache, and
// for a navigation to the cached app shell when fully offline.
try {
const response = await fetch(request);
if (response.ok && response.type === "basic") {
cache.put(request, response.clone());
}
return response;
} catch (err) {
const cached = await cache.match(request);
if (cached) return cached;
if (request.mode === "navigate") {
const shell = await cache.match(`${base}/`);
if (shell) return shell;
}
throw err;
}
})(),
);
});