Search code examples
flutterwebflutter-dependenciesservice-worker

Why new service worker is not reflecting after deploying new build? [Flutter web]


I am facing an issue with the flutter web. I am unable to get the new version of the service worker after uploading or deploying with the new web build. Only we can get the new version when we clear the web cache, Even refreshing the web also will not get the newer version, But sometimes it will load the newer version and sometimes refreshing the web will also not reflect the new service worker.

The solution tried to resolve the issue.

  1. Tried changing the version of the app in pubspec.
  2. Tried no-cache meta tag.
  3. Tried handling cache removal in index.html.
 window.addEventListener('load', function(ev) {
   // Download main.dart.js
   _flutter.loader.loadEntrypoint({
     entrypointUrl: "/main.dart.js", 
     serviceWorker: {
       serviceWorkerVersion: serviceWorkerVersion,
       serviceWorkerUrl: "/flutter_service_worker.js?v=",
     }
   }).then(function(engineInitializer) {
     return engineInitializer.initializeEngine();
   }).then(function(appRunner) {
     return appRunner.runApp();
   });
 });
</script>

I know this issue persist since flutter released the web support. I am facing this for long back days. So anyone has found a minimal solution to fix this or how we can make force refresh of the window and remove the cache?

As we know PWA is auto-updated when found the new version. Is working perfectly with flutter web but not the web page when we open it in the browser window.


Solution

  • go to web/index.html and change main.dart version :

    main.dart.js
    to 
    main.dart.js?version=your version