Skip to content

Fetch current service worker version/hashΒ #45414

@tayambamwanza

Description

@tayambamwanza

Which @angular/* package(s) are relevant/related to the feature request?

service-worker

Description

As you know debugging service worker in production can be a pain. Something that has made it easier for me is to display the hash version in the settings menu of my applications, it provides a way to view "version numbers" of deployed service workers and then I can prepare a safety-worker.js for the correct version.

So when I get a user who reports to me they are having issues, I ask them to go to settings and send me the version number there. Before I started using this method it was a pain to explain to users that they must clear cache or unregister.

Last I checked, the hash is only really emitted when update events are fired (and it is not type safe).

I would like an easier/official way of accessing the service workers hash/version to make it easier to debug and identify what version of deployed service worker is giving issues in deployed apps.

Proposed solution

A new method to get/expose the hash/version

e.g. this.swUpdate.getHash() or this.swUpdate.getCurrentVersion();

it would also be nice to perhaps have this.swUpdate.getPreviousHash();

Alternatives considered

Continue using a workaround for this value, perhaps add workaround to documentation.

Here is code I have written

 availableUpdate$: Observable<void> = this.swUpdate.available.pipe(
    map((event: UpdateAvailableEvent) => {
      const updateInfo = {
        current_version: event.current.hash,
        update_version: event.available.hash,
      };

     //  this.storage.set('pwaVersion', updateInfo.current_version).subscribe();

      this.swUpdate.activateUpdate();
    })
  );
  activatedUpdate$: Observable<void> = this.swUpdate.activated.pipe(
    map((event: UpdateActivatedEvent) => {
      const previousUpdate =
        event.previous?.hash !== undefined ? event.previous.hash : 'none';

      const successfulUpdateInfo = {
        previous_version: previousUpdate,
        current_version: event.current.hash,
      };

      // this.storage.set('pwaVersion', successfulUpdateInfo.current_version).subscribe();
    })
  );

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: service-workerIssues related to the @angular/service-worker packagefeature: under considerationFeature request for which voting has completed and the request is now under considerationhelp wantedAn issue that is suitable for a community contributor (based on its complexity/scope).workaround2: non-obvious

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions