summaryrefslogtreecommitdiff
path: root/server/lib/net/app.dart
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@spawned.biz>2026-11-23 00:51:32 +0100
committerJoel Klinghed <the_jk@spawned.biz>2026-11-23 00:51:32 +0100
commit93970f361b831f17d48ff2a345fc78488ac71fba (patch)
treef3844cc343c95dcc19a1444bdb6666e807bc59ef /server/lib/net/app.dart
parentb884441ba0d10bdd3559748507a0991888f66194 (diff)
Phase 7: hardening and shipping (M7)
E2E (Playwright, e2e/): - table fixture: one real compiled server per test (temp saves dir, free port, workers: 1); a Tab helper drives the real UI (menus, rows, initiative, draw, seats) over one WS per fresh browser context - full round: onboarding, secret initiative masked to ?? per connection, draw revealing on every screen, turns, next round, round-2 secrecy - seats: claim/re-claim on owner-leave, reload keeps the session and re-claims, two sessions can't own one character - resilience: a dropped tab resyncs on reconnect (no drift); a SIGKILL'd server restarts with state + seats surviving (incl. the owner's secret initiative and the enabled draw); a throttled tab converges on the same state as the fast tabs; a double-tapped draw runs once, error sent to the sender only - figure edit: set a character level + rename from the status menu Cross-check vs the reference (feature parity): - status menu: level pills (setCharacterLevel) + a name field (changeName), mirroring SetCharacterLevelMenu - ability-card removal: draw-pile-only "send to bottom" (reorderAbilityList -> index 0) + "shuffle undrawn cards" (shuffleDrawnAbilityCard), mirroring RemoveCardMenu - Add Character: already-added classes disabled (Objective/Escort may be added repeatedly), matching _characterAlreadyAdded - every remaining registry command verified reachable from the UI Session persistence (found by the restart test): - the client never learned its session id before the first welcome, so a reconnect minted a new id and orphaned the restored seat; fixed with establishSessionCookie() (a GET /session before the WS upgrade, plus a vite /session proxy) so the x-haven-session cookie survives restarts - the initiative input now prefills from re-synced state (CharacterRow effect, guarded against clobbering a live edit) so a reload/reconnect doesn't blank an entry Performance + deployment: - immutable asset caching (Cache-Control: 1yr, immutable) for the game data / images / in-game translations; no-cache for the SPA index.html so rebuilt apps reach open tabs - server tool/perf_measure.dart: the performance probe (state payload size at full-table scale, WS fan-out bytes per action, traffic per connection) - Dockerfile (server + built web); docs/DEPLOYMENT.md (Docker, persistence, reverse proxy, measured performance, smoke-test list) - README quickstart + production/Docker/testing sections Server: - --saves-dir and --data-dir CLI options (Docker / multi-game hosts), threaded through the stores and data loading Misc: - app.css: height 100% on .row so the turn-done tap zone resolves to a clickable height; fake-socket.ts readyState constants corrected to the real WebSocket values (0 CONNECTING, 1 OPEN) - scripts/ci.sh: the Playwright E2E gate (runs when PLAYWRIGHT is set) - PLAN.md: Phase 7 boxes checked, M7 progress-log entry Verification: svelte-check 0 errors; web 196/196 tests; vite build OK; dart analyze 0 errors / 0 warnings; dart test 604/604; Playwright 9/9; scripts/ci.sh green end-to-end.
Diffstat (limited to 'server/lib/net/app.dart')
-rw-r--r--server/lib/net/app.dart55
1 files changed, 47 insertions, 8 deletions
diff --git a/server/lib/net/app.dart b/server/lib/net/app.dart
index 8936104..a57aab0 100644
--- a/server/lib/net/app.dart
+++ b/server/lib/net/app.dart
@@ -18,6 +18,20 @@ import 'ws_upgrade.dart';
final _log = Logger('server');
+/// One year, in seconds — the cache lifetime for immutable assets
+/// (PLAN.md Phase 7 "Performance").
+const int _kCacheYearSeconds = 60 * 60 * 24 * 365;
+
+/// `Cache-Control` for assets that are never mutated in place: the game
+/// data, images, in-game translations, and the content-hashed build
+/// outputs. Browsers keep them for a year and never revalidate.
+const String _kImmutableCacheControl =
+ 'public, max-age=${_kCacheYearSeconds}, immutable';
+
+/// `Cache-Control` for the SPA entry point: always revalidate, so a
+/// rebuilt app reaches open tabs on the next navigation.
+const String _kNoCacheControl = 'no-cache';
+
/// Builds the HTTP handler for the game server.
///
/// [dataDir] (e.g. `assets/data/`) is served read-only at `/data/` when
@@ -131,12 +145,18 @@ Handler _createStaticHandler(String staticDir, {bool plain = false}) {
);
}
if (plain) {
- // Plain static serving (no SPA fallback) with long-lived caching —
- // the JSON and images are immutable build inputs.
- return createStaticHandler(
- directory.path,
- maxAge: const Duration(days: 1),
- );
+ // Plain static serving (no SPA fallback) for the game data, images, and
+ // in-game translations — immutable build inputs: cached for a year
+ // without revalidation (the 66 MB image set is the big win here).
+ final inner = createStaticHandler(directory.path);
+ return (Request request) async {
+ final response = await inner(request);
+ if (response.statusCode == 200) {
+ return response
+ .change(headers: {'cache-control': _kImmutableCacheControl});
+ }
+ return response;
+ };
}
final staticHandler = createStaticHandler(
directory.path,
@@ -147,14 +167,33 @@ Handler _createStaticHandler(String staticDir, {bool plain = false}) {
if (response.statusCode == 404 &&
request.method == 'GET' &&
!request.url.path.contains('.')) {
- // SPA fallback: routes without a file extension get index.html.
+ // SPA fallback: routes without a file extension get index.html —
+ // no-cache, like the real file, so deploys reach open tabs.
final indexFile = File(p.join(directory.path, 'index.html'));
if (await indexFile.exists()) {
return Response.ok(
await indexFile.readAsString(),
- headers: {'content-type': 'text/html; charset=utf-8'},
+ headers: {
+ 'content-type': 'text/html; charset=utf-8',
+ 'cache-control': _kNoCacheControl,
+ },
);
}
+ return response;
+ }
+ if (response.statusCode == 200) {
+ // index.html is revalidated every navigation; everything else in the
+ // build is content-hashed (or a stable font) and immutable.
+ // (shelf_router strips the `/` mount prefix, so the handler sees
+ // `index.html` / `` rather than `/index.html` / `/`.)
+ final path = request.url.path;
+ final isIndex =
+ path.isEmpty || path == '/' || path.endsWith('index.html');
+ return response.change(
+ headers: {
+ 'cache-control': isIndex ? _kNoCacheControl : _kImmutableCacheControl,
+ },
+ );
}
return response;
};