// Tutorial //

Angular Service Worker: Dealing With Updates

Published on December 13, 2017
Default avatar
By Alligator.io
Developer and author at DigitalOcean.
Angular Service Worker: Dealing With Updates

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

If you’ve setup a service worker for your app using the @angular/service-worker package, you may be wondering about how to deal with stale versions of your app for end users. This can easily become a problem because new versions of a service worker will only be activated on page reload.

Thankfully, @angular/service-worker has a SwUpdate class that makes it easy to check for available updates.

Subscribing to Available Updates

Let’s go over SwUpdate’s basic usage by creating an Update service that subscribes to the available observable, which emits when there’s a service worker update available:

update.service.ts
import { Injectable } from '@angular/core';

import { SwUpdate } from '@angular/service-worker';

A simple page reload will do the trick to activate the new service worker, so the update logic could use something like a snackbar component to prompt the user to reload the page. Angular Material has a great snackbar component that could be used like the following:

update.service.ts
import { Injectable } from '@angular/core';

import { MatSnackBar } from '@angular/material';
import { SwUpdate } from '@angular/service-worker';
@Injectable()
export class UpdateService {
  constructor(private swUpdate: SwUpdate, private snackbar: MatSnackBar) {
    this.swUpdate.available.subscribe(evt => {
      const snack = this.snackbar.open('Update Available', 'Reload');
  snack
    .onAction()
    .subscribe(() => {
      window.location.reload();
    });

  snack.setTimeout(() => {
    snack.dismiss();
  }, 6000);
});

Then we’d just have to make sure that our Update service is provided in the app module and that it’s injected in the app component:

app.component.ts
import { Component } from '@angular/core';

import { UpdateService } from './update.service';

And that’s it for a basic update mechanism. Let’s now have a look at a few more properties and methods available as part of the SwUpdate class.

activated & isEnabled

SwUpdate has an activated observable that’s similar to the available observable and that can be subscribed to in order to hook onto successful service worker activations.

Additionally, there’s an isEnabled property that returns true if the service worker is currently enabled:

import { Injectable } from '@angular/core';
import { SwUpdate } from '@angular/service-worker';

@Injectable()
export class UpdateService {
  constructor(private swUpdate: SwUpdate) {
    if (!this.swUpdate.isEnabled) {
      console.log('Nope 🙁');
    }
  }
}

checkForUpdate() & activateUpdate()

The SwUpdate class also has two methods that allow us more control over service worker updates:

  • checkForUpdate(): Allows to check for updates periodically.
  • activateUpdate(): Allows us to force a service worker update.

If you’ve enjoyed this tutorial and our broader community, consider checking out our DigitalOcean products which can also help you achieve your development goals.

Learn more here


About the authors
Default avatar
Developer and author at DigitalOcean.

Still looking for an answer?

Was this helpful?
Leave a comment

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!