Service Worker loading stale / outdated cached files (cli 3.11.5 / sdk 35 and cli 3.11.7 / sdk 36)

Whenever I upload a new build to our server, any browser that had opened the previous version continues to use that version unless the cache is cleared, or a full refresh is triggered. Even then, refreshing normally after that will sometimes go back to the older version. This seems to trace back to the service worker. When an old version is erroneously loaded, the Chrome network log says it was served by the service worker. This sounds very similar to https://github.com/expo/web-examples/issues/60 which was supposed to be fixed in https://github.com/expo/expo-cli/pull/1082 but even after updating to cli 3.11.5, clearing all my browser data, making sure it always loads a version from after that updated build, and then doing another update, the problem persists.

This was on @expo/webpack-config@0.10.1, I just noticed there’s a newer version and it generates a different register-service-worker.js. I’ll report back if @expo/webpack-config@0.10.11 fixes the problem.

That did not fix anything. Even forcing the app to refresh if a newer version is detected on the server doesn’t work. Why does Expo use service workers anyway? Is there a way to NOT use service workers?

For what it’s worth, I’ve been running into the same problem and have been unsuccessful in fixing it. IMO, the default caching policies that Expo passes to Workbox for caching assets are way too aggressive for defaults.

This should have been fixed for a while now. I think the unfortunate nature of this error has caused any legacy service workers to remain registered. ⌘+shift+R or manually unregistering service workers should remove the legacy aggressive workers.

It continues even after unregistering the service workers. The new ones are just as aggressive.

1 Like

I’m actually also facing this still, for a project I just created.

Final solution: Disable offline mode: https://www.npmjs.com/package/@expo/webpack-config#fully-disabling-the-service-worker


My first attempted solution (for deploying to netlify) was to add a _headers file to the deployment folder. This didn’t work.

"scripts": {
 ...,
 "predeploy: "expo build:web && yarn headers",
  "headers": "echo \"/*\n  cache-control: max-age=0\n  cache-control: no-cache\n  cache-control: no-store\n  cache-control: must-revalidate\" > web-build/_headers",
  "deploy": "netlify deploy --prod"
}
1 Like

I’m afraid it wasn’t fixed. I deployed my first app (via Vercel) just a week ago for the very first time, so there definitely wasn’t any old version hanging around.

P.S. SDK 37

It’s still reproducing for me. I serve it using express app and building with --no-pwa flag. SDK 37.

solution provided by @nandorojo doesn’t work for me either

I was able to kill all registered service workers by replacing the code in expo-service-worker.js with

self.addEventListener('install', function (e) {
  self.skipWaiting();
});

self.addEventListener('activate', function (e) {
  self.registration
    .unregister()
    .then(function () {
      return self.clients.matchAll();
    })
    .then(function (clients) {
      clients.forEach((client) => client.navigate(client.url));
    });
});