Skip to content

Commit 1b0b38c

Browse files
committed
Sideline the skwasm heap race and serve engine and fonts from our origin
The e2e renderer hang is a wasm heap race in multi-threaded skwasm (filed as flutter/flutter#190039, reproduced in seconds by e2e/skwasm-repro); web ships single-threaded until it is fixed, with a hang-evidence probe under every spec and a knob to re-test new engines. The same loader config takes the engine off Google's CDN, the owned font chain now covers CJK (full face, deferred like Arabic, Hebrew, and Thai, warmed by an API-transport interceptor), the engine's fallback probes 404 on our origin, and the server answers navigations with the shell, subresource misses with 404s, and repeat fetches with 304s.
1 parent ebbf10f commit 1b0b38c

57 files changed

Lines changed: 1653 additions & 173 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎app/app/lib/src/app.dart‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ class WaxDeckApp extends ConsumerWidget {
1414
seedColor: const Color(0xFFD9A648),
1515
brightness: brightness,
1616
),
17+
// The bundled type with the owned fallback chain. Without a named
18+
// family, web text asks for Roboto from Google's CDN, and without
19+
// the chain any non-Latin metadata does the same, which a LAN-only
20+
// instance cannot answer; the design system's fonts serve both from
21+
// WaxDeck's own origin (deferred faces load via warmFontsFor).
22+
fontFamily: WaxFonts.uiQualified,
23+
fontFamilyFallback: WaxFonts.fallbacksQualified,
1724
);
1825

1926
@override

‎app/app/lib/src/fonts_warmup.dart‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import 'package:dio/dio.dart';
2+
import 'package:waxdeck_ui/waxdeck_ui.dart';
3+
4+
/// Starts loading any on-demand script faces (Arabic, Hebrew, Thai, CJK)
5+
/// the server's metadata will need on screen.
6+
///
7+
/// One interceptor on the API transport instead of a warm call in every
8+
/// controller: each response body is walked once, right where every DTO
9+
/// arrives, so no screen can forget its corner of the library. The walk
10+
/// is fire-and-forget (rendering never waits on a font; text that raced
11+
/// ahead of its face re-lays out when the loader lands) and
12+
/// [WaxFonts.scriptsIn] stops reporting a script once its load has
13+
/// started, so the cost decays to a near-empty scan per string once a
14+
/// library's faces are in.
15+
class FontWarmupInterceptor extends Interceptor {
16+
@override
17+
void onResponse(
18+
Response<Object?> response,
19+
ResponseInterceptorHandler handler,
20+
) {
21+
_scan(response.data);
22+
handler.next(response);
23+
}
24+
25+
void _scan(Object? node) {
26+
if (node is String) {
27+
for (final script in WaxFonts.scriptsIn(node)) {
28+
WaxFonts.ensureScript(script);
29+
}
30+
} else if (node is List<Object?>) {
31+
node.forEach(_scan);
32+
} else if (node is Map<Object?, Object?>) {
33+
node.values.forEach(_scan);
34+
}
35+
}
36+
}

‎app/app/lib/src/providers.dart‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import 'package:dio/dio.dart';
12
import 'package:flutter/foundation.dart';
23
import 'package:flutter_riverpod/flutter_riverpod.dart';
34
import 'package:waxdeck_api/waxdeck_api.dart';
45
import 'package:waxdeck_player/waxdeck_player.dart';
56

67
import 'auth/credential_store.dart';
8+
import 'fonts_warmup.dart';
79
import 'auth/loopback/loopback.dart';
810
import 'auth/oidc_flow.dart';
911
import 'auth/oidc_ports.dart';
@@ -21,8 +23,16 @@ String get waxDeckBaseUrl =>
2123
_envBase.isNotEmpty ? _envBase : (kIsWeb ? '' : 'http://localhost:4420');
2224

2325
/// The one API repository. Tests override this with a fake.
26+
///
27+
/// The transport carries the font-warmup interceptor: every response is
28+
/// where library metadata first arrives, so the deferred script faces
29+
/// (Arabic, Hebrew, Thai, CJK) start loading before any screen lays the
30+
/// text out, with no per-screen wiring to forget.
2431
final repositoryProvider = Provider<WaxDeckRepository>(
25-
(ref) => WaxDeckClient(baseUrl: waxDeckBaseUrl),
32+
(ref) => WaxDeckClient(
33+
baseUrl: waxDeckBaseUrl,
34+
dio: Dio()..interceptors.add(FontWarmupInterceptor()),
35+
),
2636
);
2737

2838
/// Where the bearer token persists between launches. Web builds keep

‎app/app/pubspec.yaml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ dependencies:
2424
# Routing. Published by flutter.dev and pure Dart, but pinned exact like
2525
# every other dependency: the route table is the app's navigation
2626
# contract and a minor bump has moved redirect semantics before.
27+
# Direct dependency for the font-warmup interceptor on the API
28+
# transport (waxdeck_api already rides this exact version).
29+
dio: 5.10.0
2730
go_router: 17.3.0
2831
url_launcher: 6.3.2
2932
web: 1.1.1

‎app/app/web/index.html‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,37 @@
120120
}
121121
}, 20000);
122122
</script>
123-
<script src="flutter_bootstrap.js" async></script>
123+
<script>
124+
{{flutter_js}}
125+
{{flutter_build_config}}
126+
127+
// The loader call is owned here rather than left to the generated
128+
// one-line bootstrap, because two defaults are wrong for WaxDeck:
129+
// the engine would load from Google's CDN (this page's header says
130+
// nothing here may reach the network, and the server embeds the
131+
// whole canvaskit directory), and multi-threaded skwasm corrupts
132+
// its shared heap under concurrent text layout and rasterization,
133+
// freezing the page at full CPU. Single-threaded until
134+
// flutter/flutter#190039 is fixed; e2e can flip threading back on
135+
// per run (WAXDECK_E2E_MT_SKWASM=1) to re-test a new engine, and
136+
// e2e/skwasm-repro answers the same question in seconds.
137+
_flutter.loader.load({
138+
serviceWorkerSettings: {
139+
serviceWorkerVersion: {{flutter_service_worker_version}},
140+
},
141+
config: {
142+
canvasKitBaseUrl: "canvaskit",
143+
forceSingleThreadedSkwasm: true,
144+
// The engine's last-resort font machinery (its Roboto default
145+
// and per-glyph Noto shards) downloads from fonts.gstatic.com
146+
// by default. Pointing it at this origin keeps an air-gapped
147+
// instance identical to an online one: the requests 404, the
148+
// engine degrades, and the owned chain in waxdeck_ui is what
149+
// actually renders. The path resolves under assets/ and is
150+
// deliberately unrouted.
151+
fontFallbackBaseUrl: "wax-font-fallback/",
152+
},
153+
});
154+
</script>
124155
</body>
125156
</html>

app/packages/waxdeck_ui/fonts/NotoSansArabic-Variable.ttf renamed to app/packages/waxdeck_ui/assets/fonts/NotoSansArabic-Variable.ttf

File renamed without changes.
15.7 MB
Binary file not shown.

app/packages/waxdeck_ui/fonts/NotoSansHebrew-Variable.ttf renamed to app/packages/waxdeck_ui/assets/fonts/NotoSansHebrew-Variable.ttf

File renamed without changes.

app/packages/waxdeck_ui/fonts/NotoSansThai-Variable.ttf renamed to app/packages/waxdeck_ui/assets/fonts/NotoSansThai-Variable.ttf

File renamed without changes.
Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
# On-demand fonts
22

33
Fonts here are **not** declared in `pubspec.yaml`'s `fonts:` section: they
4-
are loaded at runtime by `WaxFonts.ensureCjk()` from WaxDeck's own origin,
5-
so a build pays their download only when the locale or the metadata on
6-
screen actually needs them.
7-
8-
`NotoSansCJK.ttf` is the one such face. It is an order of magnitude larger
9-
than the whole eager chain, and most instances never render a Han glyph,
10-
so it stays out of the startup path. Drop a build of it here (family name
11-
`NotoSansCJK`) and `WaxFonts.ensureCjk()` picks it up; with the file
12-
absent, the loader degrades to the platform's own fallback, which is what
13-
a build without it would have done anyway.
4+
are plain assets, loaded at runtime from WaxDeck's own origin by
5+
`WaxFonts.ensureFor` (or `ensureScript`) the first time the text on
6+
screen needs their script. Startup pays only for the eager chain in
7+
`../../fonts/`; an all-Latin library never downloads any of these.
8+
9+
Four faces live here, produced by `tools/fetch-fonts.sh`:
10+
11+
- `NotoSansArabic-Variable.ttf`, `NotoSansHebrew-Variable.ttf`,
12+
`NotoSansThai-Variable.ttf`: subsets, moved out of the eager chain
13+
because together they cost 750 KB of startup on libraries that mostly
14+
never show those scripts.
15+
- `NotoSansCJK.otf`: the full Noto Sans CJK SC face, deliberately not
16+
subset. A curated "common hanzi" core still renders boxes for any name
17+
outside it, which is the failure this asset exists to close; one
18+
regional face carries Han, kana, and hangul alike. It is an order of
19+
magnitude larger than everything else combined, which is why it (and
20+
the whole directory) loads on demand.
21+
22+
With a file absent, `WaxFonts` degrades to the platform's own fallback,
23+
which is what a build without the asset would have done anyway.

0 commit comments

Comments
 (0)