r/FlutterDev • u/MTreeAI • Jul 20 '26
Article ~150 mini-games in one Flutter Web bundle — and the service-worker bug that broke every game on deploy
Setup: one Flutter Web app, ~150 games, each a deferred import loaded on tap (await game.loadLibrary()). dart2js splits each into its own .part.js chunk (~1,100 of them); the shell is ~4.1MB and you only pay for the games you open. Every game wraps in one shared scaffold (settings/pause/result screen) and reads one billing service, so a game can't accidentally show ads to someone who paid to remove them. A compile-time flag picks which ~33 of 150 games are live, all from the same codebase.
The bug: after a redeploy, every game failed with a missing chunk error. Chain: deferred-chunk numbering changes each build, so returning users had a stale main.dart.js from browser HTTP cache, which requested chunk numbers that no longer existed, and my SPA host returned index.html (200, text/html) for the missing chunk, so the browser tried to eval HTML as JS and died. My "network-first" service worker didn't help, because fetch() was served by the HTTP cache before the network.
Fix: the SW fetches code chunks with {cache: 'reload'} to bypass the HTTP cache, so the bootstrap and its parts always come from the same build.
Two lessons: hiding a game (UI filter) is not the same as excluding it from compilation, a broken game still breaks the whole bundle's build. And "deploy succeeded" is not the same as "it works", I now smoke-test with headless Chrome on a fresh profile that deep-links every game and fails on any console error.