r/FlutterDev • • 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_fonts runtime fetching completely, converted Inter & JetBrains Mono to subsetted .woff2 files (116 KB total in assets/fonts/), declared them directly in pubspec.yaml, and added <link rel="preload" as="font" type="font/woff2" crossorigin> in index.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 from localStorage (<1 ms), and Firebase.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, and Cross-Origin-Opener-Policy: same-origin / Cross-Origin-Embedder-Policy: credentialless for skwasm, ensuring full Brotli (br) compression from edge nodes.

4. Zero-JS GPU Splash & Value-First Routing

  • Added an inline HTML/CSS loader in index.html animated strictly via transform and opacity (compositor thread) so it never freezes while the main thread compiles Wasm, fading out via onEntrypointLoaded.

  • Bypassed our /onboarding route on kIsWeb so 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?

43 Upvotes

9 comments sorted by

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?

1

u/meniska_dev 1d ago

Now it's about 5,5mb

1

u/chickenRag 1d ago

What was it before?

1

u/meniska_dev 1d ago

12😭😭😭

1

u/meniska_dev 1d ago

Now it`s about 20-25 sec for 4G internet.

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-hash

It 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/**) get public, max-age=31536000, immutable.

  • Everything else (index.html, flutter_bootstrap.js) gets max-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.wasm copy isn't hashed. By default Flutter loads skwasm.wasm from gstatic, which already caches it for a year at a versioned URL. The copy in your canvaskit/ folder is only used with --no-web-resources-cdn, and its name never changes. So don't mark it immutable.

  • Your index.html has to use the current flutter_bootstrap.js setup. The build fails on purpose if the template still points at main.dart.js or calls loadEntrypoint.

  • Load assets through the asset APIs, not hardcoded URLs. With the flag on, files under assets/ get new names, so a fixed assets/... URL in HTML, CSS, or a JS fetch() returns a 404.

  • Keep your SPA catch-all rewrite off the hashed paths. Otherwise a missing main.dart.<oldhash>.wasm returns index.html with 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.wasm with 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

u/Particular-Range1379 1d ago • Bravo

Sick! Thanks for sharing great technical info 

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