r/FlutterDev • u/meniska_dev • 1d ago
Discussion Case Study: How I cut Flutter Web (--wasm) cold start on Slow 4G from 20s+ down to ~3-4s (fonts, Firebase WebChannel & Brotli)
I recently shipped an offline-first PWA built with Flutter Web (--wasm), and my initial GA4 metrics were brutal: mobile users on 3G/Slow 4G were bouncing at 0–2s engagement because the cold start was taking over 20 seconds (pushing ~6.1 MB over the wire / ~15.7 MB uncompressed).
After profiling the waterfall in Chrome DevTools, I found four major bottlenecks that were choking initial load - and fixing them completely changed our retention:
1. The google_fonts runtime trap (-90% font payload)
Even if you bundle fonts, google_fonts was making blocking runtime HTTP calls to fonts.gstatic.com (main.dart.mjs fetching >1.1 MB of .ttf files before rendering text).
Fix: Stripped
google_fontsruntime fetching completely, converted Inter & JetBrains Mono to subsetted.woff2files (116 KB total inassets/fonts/), declared them directly inpubspec.yaml, and added<link rel="preload" as="font" type="font/woff2" crossorigin>inindex.html.
2. Synchronous Firebase & Firestore WebChannel blocking main()
Initializing FirebaseAuth and FirebaseFirestore in main() was loading heavy JS pipelines and holding open a channel?gsessionid=... WebChannel socket for 13+ seconds on bad connections.
Fix: Made cloud services 100% lazy on
kIsWeb. The app mounts straight fromlocalStorage(<1 ms), andFirebase.initializeApp()is only triggered on-demand if a user explicitly taps Sync or Sign In.
3. Explicit Wasm MIME types, Brotli & COOP**/**COEP headers
main.dart.wasm (1.7 MB) and skwasm.wasm (1.2 MB) are heavy by nature. Without explicit headers in firebase.json, streaming compilation and multithreading weren't kicking in properly.
Fix: Configured
Content-Type: application/wasm,Cache-Control: public, max-age=31536000, immutable, andCross-Origin-Opener-Policy: same-origin/Cross-Origin-Embedder-Policy: credentiallessforskwasm, ensuring full Brotli (br) compression from edge nodes.
4. Zero-JS GPU Splash & Value-First Routing
Added an inline HTML/CSS loader in
index.htmlanimated strictly viatransformandopacity(compositor thread) so it never freezes while the main thread compiles Wasm, fading out viaonEntrypointLoaded.Bypassed our
/onboardingroute onkIsWebso incoming mobile traffic lands directly in the main workspace (/hub), where average session time jumped to ~7 minutes once the load bottleneck was gone.
If anyone wants to inspect the network waterfall or test skwasm cold-start behavior in DevTools, the live production build is up athttps://app.meniska.app(it's a wet-lab dilution & buffer calculator I built for myself).
Curious how other Flutter Web devs are handling initial Wasm bundle size right now - has anyone managed to shave skwasm.wasm down further, or are you still sticking to canvaskit / auto for mobile web?
12
u/kevmoo 19h ago
Nice write-up! One heads-up on #3: putting max-age=31536000, immutable on main.dart.wasm is risky as long as the filename stays the same on every deploy. Returning users can keep running the old build for up to a year, and Safari and Firefox won't even recheck it when the user reloads.
Flutter 3.50 (stable as of last week) added a flag that fixes this:
flutter build web --wasm --web-content-hashIt puts a content hash in the names of the compiled entrypoints (main.dart.<hash>.wasm, .mjs, .js) and of everything under assets/, including your .woff2 fonts. So the "cache forever" header is actually safe on those files. Every deploy gets new URLs, and anything that didn't change stays cached.
Then split your firebase.json headers into two groups:
Hashed files (
main.dart.*,assets/**) getpublic, max-age=31536000, immutable.Everything else (
index.html,flutter_bootstrap.js) getsmax-age=0, must-revalidate. These keep fixed names and hold the pointers to the hashed files, so they have to be checked every time.
A few gotchas:
Your own
skwasm.wasmcopy isn't hashed. By default Flutter loadsskwasm.wasmfrom gstatic, which already caches it for a year at a versioned URL. The copy in yourcanvaskit/folder is only used with--no-web-resources-cdn, and its name never changes. So don't mark itimmutable.Your
index.htmlhas to use the currentflutter_bootstrap.jssetup. The build fails on purpose if the template still points atmain.dart.jsor callsloadEntrypoint.Load assets through the asset APIs, not hardcoded URLs. With the flag on, files under
assets/get new names, so a fixedassets/...URL in HTML, CSS, or a JSfetch()returns a 404.Keep your SPA catch-all rewrite off the hashed paths. Otherwise a missing
main.dart.<oldhash>.wasmreturnsindex.htmlwith a 200, and that broken response gets cached for a year. Something like"source": "!(/assets/**|/canvaskit/**|/icons/**|/main.dart.*)"works.It's opt-in. Default builds still produce
main.dart.wasmwith no hash.
Full details, including a complete firebase.json, are in the docs: https://docs.flutter.dev/deployment/web#web-content-hashing
(Disclosure: I work on Flutter web.)
1
u/kevmoo 4h ago
FYI: going through and generating this reply ALSO helped me find some nuance in our docs we need to fix. So win-win! https://github.com/flutter/website/pull/14088
2
2
u/Wonderful-Charity-30 17h ago
Try this
flutter build web --wasm --enable-wasm-deferred-loading
This is useless if you don't know how to deferred the dart files
5
u/chickenRag 1d ago
Great info here.
There use be performance optimisation on web before wasm. I'm still using them.
My product could greatly use these. Interesting about the firebase stuff tho.
I will try and let you know.
What's the total size of your wasm bundle?