0

I have an Angular 14 SPA hosted on Firebase and I have the following issue:
The users are using the cached version (previous version) instead of the latest version when accessing the application (website).

Steps to reproduce the issue:

  • User opens application - let's say v1.0.0
  • I deploy a new version - let's say v2.0.0
  • User opens application again and he is using v1.0.0 (from cache I guess) (if the user refreshes the page, v2.0.0 is loaded)

Expected behavior:

  • User opens application - let's say v1.0.0
  • I deploy a new version - let's say v2.0.0
  • User opens application again and he is using v2.0.0 (no refresh needed)

Initially, I thought the PWA service worker causes the issue.
I understood that the PWA service worker gets the new version after the application is opened (similar to a mobile application) and updates the application on the next lunch - and that I could show a dialog to notify the new version (based on swUpdate.available.subscribe - https://stackoverflow.com/a/66905807) and after the user's confirmation (or even without his confirmation) to force the reload using location.reload() but I didn't want to take this approach so I decide to remove PWA.

The application is now just a SPA (no PWA) and I made sure the service worker is unregistered as shown below (also checked in the browser and the application doesn't have service worker, manifest, etc) but I still have the same issue.

if ("caches" in window) {
  caches.keys().then(function (keyList) {
    return Promise.all(
      keyList.map(function (key) {
        return caches.delete(key);
      })
    );
  });
}

if (window.navigator && navigator.serviceWorker) {
  navigator.serviceWorker.getRegistrations().then(function (registrations) {
    for (let registration of registrations) {
      registration.unregister();
    }
  });
}

This is my build script:

{
  ...
  "build": "ng build --configuration production --aot --output-hashing=all"
  ...
}

1 Answers1

0

I found the issue - Firebase was sending in the response headers: Cache-Control: max-age=3600.

I solved the issue by setting the headers in firebase.json:

{
  ...
  "headers": [
    {
      "source": "**",
      "headers": [
        {
          "key": "Cache-Control",
          "value": "no-cache, no-store, must-revalidate"
        }
      ]
    },
    {
      "source": "**/*.@(ico|svg|jpg|jpeg|png|gif)",
      "headers": [
        {
          "key": "Cache-Control",
          "value": "max-age=3600"
        }
      ]
    },
    {
      "source": "**/*.@(eot|otf|ttf|ttc|woff|woff2|font.css)",
      "headers": [
        {
          "key": "Cache-Control",
          "value": "max-age=31536000"
        }
      ]
    }
  ]
  ...
}

I found this article and the Firebase documentation useful.