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.
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:
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:
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:
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.
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 🙁');
}
}
}
The SwUpdate class also has two methods that allow us more control over service worker updates:
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
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.
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!