summaryrefslogtreecommitdiff
path: root/server/lib/net/app.dart
diff options
context:
space:
mode:
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;
};