fix(service-worker): check for updates on navigation

Currently the Service Worker checks for updates only on SW startup,
an event which happens frequently but also nondeterministically. This
makes it hard for developers to observe the update process or reason
about how updates will be delivered to users. This problem is
exacerbated by the DevTools behavior of keeping the SW alive
indefinitely while opened, effectively preventing the page from
updating at all.

This change causes the SW to additionally check for updates on
navigation requests (app page reloads). This creates deterministic
update behavior, and is much easier for developers to reason about.
It does leave the old update-on-SW-startup behavior in place, as
removing that would be a breaking change.

Fixes #20877
This commit is contained in:
Alex Rickabaugh
2017-12-14 13:10:06 -08:00
parent 0b2d636b75
commit 20e1cc049f
3 changed files with 55 additions and 8 deletions

View File

@ -88,6 +88,11 @@ export class Driver implements Debuggable, UpdateSource {
private lastUpdateCheck: number|null = null;
/**
* Whether there is a check for updates currently scheduled due to navigation.
*/
private scheduledNavUpdateCheck: boolean = false;
/**
* A scheduler which manages a queue of tasks that need to be executed when the SW is
* not doing any other work (not processing any other requests).
@ -327,6 +332,15 @@ export class Driver implements Debuggable, UpdateSource {
return this.safeFetch(event.request);
}
// On navigation requests, check for new updates.
if (event.request.mode === 'navigate' && !this.scheduledNavUpdateCheck) {
this.scheduledNavUpdateCheck = true;
this.idle.schedule('check-updates-on-navigation', async() => {
this.scheduledNavUpdateCheck = false;
await this.checkForUpdate();
});
}
// Decide which version of the app to use to serve this request. This is asynchronous as in
// some cases, a record will need to be written to disk about the assignment that is made.
const appVersion = await this.assignVersion(event);