Tutorial

Angular Service Worker: Dealing With Updates

Published on December 13, 2017
Default avatar

By Alligator.io

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.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Alligator.io

author

Still looking for an answer?

Ask a questionSearch for more help

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!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel