Skip to content

Fetch current service worker version/hash #45414

Closed as not planned
Closed as not planned

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();
    })
  );

Activity

  1. gkalpak commented on Mar 23, 2022

    @gkalpak
    Member

    Adding a method to get the current (and/or latest) version sounds reasonable to me.

    BTW, a better work-around imo would be to fetch ngsw/state and parse it:

    const debugInfo = await fetch('ngsw/state').then(res => res.text());
    const swVersion = /Driver version: (.+)/.exec(debugInfo)[1];
    const latestAppVersionHash = /Latest manifest hash: (.+)/.exec(debugInfo)[1];
  2. added
    featureLabel used to distinguish feature request from other issues
    area: service-workerIssues related to the @angular/service-worker package
    on Mar 23, 2022
  3. modified the milestone: Backlog on Mar 23, 2022
  4. tayambamwanza commented on Mar 23, 2022

    @tayambamwanza
    Author

    @gkalpak thanks that would be much appreciated and thanks for the tip as well, I'll give it a shot.

  5. peternixey commented on Mar 23, 2022

    @peternixey

    This is so weird - I came to the issues page to ask literally exactly this question and it was the top one here. How bizarre 😂.
    (I'd already previously asked this on StackOverflow too).

    I did a little bit of research into how the ngsw/state file is generated and it appears that the version is taken from the Service Worker Debugger object:

    import {Debuggable, DebugLogger} from './api';
    ...
    
    export class DebugHandler implements DebugLogger {
      ...
      constructor(readonly driver: Debuggable, readonly adapter: Adapter) {}
    
      async handleFetch(req: Request): Promise<Response> {
        const [state, versions, idle] = await Promise.all([
          this.driver.debugState(),
          this.driver.debugVersions(),
          this.driver.debugIdleState(),
        ]);
    
    Driver version: ${SW_VERSION}
    Driver state: ${state.state} (${state.why})
    Latest manifest hash: ${state.latestHash || 'none'}
    Last update check: ${this.since(state.lastUpdateCheck)}`;
    ...

    As far as I can tell though this object isn't exported generally into Angular. Perhaps if it were then we could access that directly rather than having to parse the page it produces?

  6. angular-robot commented on Mar 23, 2022

    @angular-robot
    Contributor

    This feature request is now candidate for our backlog! In the next phase, the community has 60 days to upvote. If the request receives more than 20 upvotes, we'll move it to our consideration list.

    You can find more details about the feature request process in our documentation.

  7. gkalpak commented on Mar 23, 2022

    @gkalpak
    Member

    As far as I can tell though this object isn't exported generally into Angular. Perhaps if it were then we could access that directly rather than having to parse the page it produces?

    You are right that's it's the DebugHandler that generates the ngsw/state, but it retrieves the info we are talking about from the main SW class (Driver). So, it is not possible to expose the instance directly to the page.

    It is possible, though, to expose a way to retrieve this info from the SW (and hide it behind an SwUpdate method for consuming from the app. (This is what we were discussing with @tayambamwanza above 😉)

    This is not something that is likely to be prioritized any time soon (if ever), but if anyone wants to work on a PR I would be happy to help/review.

  8. changed the title [-]Fetch current service worker hash easily[/-] [+]Fetch current service worker version/hash[/+] on Mar 24, 2022
  9. tayambamwanza commented on Mar 24, 2022

    @tayambamwanza
    Author

    @gkalpak I don't mind taking it on but maybe since @peternixey has been looking into this for a while maybe he's better equipped to take it on. @peternixey let me know if you want to take it on otherwise I will put something together.

  10. peternixey commented on Mar 24, 2022

    @peternixey

    @tayambamwanza I'd be delighted if you wanted to take it. I'm not yet familar with the comms structure of how the service worker communicates with the app so only on top of it to the extent of knowing what the problem is :)

    BTW if you did get to tackle this it would be super to be able to surface the service woker AppData JSON to the app too. I'm writing that using the CI process but again there's no clear way to bubble it up into the app unless the version changes.

  11. tayambamwanza commented on Mar 25, 2022

    @tayambamwanza
    Author

    @peternixey @gkalpak Ok I'll take on this as a side project, I've never done a pull request to Angular repo so forgive me if I don't get it right first time round.

  12. 13 remaining items

  13. dylhunn commented on Sep 29, 2022

    @dylhunn
    Contributor

    If there are no unforeseen complications, we'd likely accept a PR for this.

  14. artemv01 commented on Oct 23, 2022

    @artemv01

    hi @gkalpak , do you think adding an additional url like 'ngsw/state/latest-hash' that acts just like 'ngsw/state' but only returns the hash is acceptable? Or we rather better off adding a new method to SwUpdate that utilizes CacheDatabase?

  15. modified the milestones: needsTriage, Backlog on Nov 16, 2022
  16. JeanMeche commented on Mar 18, 2023

    @JeanMeche
    Member

    I've opened #49474, let me know what you think !

  17. tayambamwanza commented on Mar 20, 2023

    @tayambamwanza
    Author

    @JeanMeche thank you, haven't had time to get around to this, is it in observable because you are waiting for the value to be retrieved or for some other reason? I thought the hash only changed whenever app is built so thought it would basically be instantly available.

  18. JeanMeche commented on Mar 20, 2023

    @JeanMeche
    Member

    The hash is generated by the service-worker (it's a sha1 of the manifest).

    Since the value can be updated (when a new version is available), I made it an observable.

  19. SkyZeroZx commented on Aug 2, 2025

    @SkyZeroZx
    Contributor

    Is there any update on this? I saw the PR but it didn't get merged?

  20. JeanMeche commented on Aug 29, 2025

    @JeanMeche
    Member

    With #63364 we've announced that the service-worker package in now in maintenance mode and will not receive feature PRs and feature requests.

    The message is also visible here: https://angular.dev/ecosystem/service-workers

  21. angular-automatic-lock-bot commented on Sep 29, 2025

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  22. locked and limited conversation to collaborators on Sep 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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 considerationopen for contributionsAn 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