Skip to content

Instantly share code, notes, and snippets.

@Security2431
Forked from jeffposnick/register-sw.js
Created July 18, 2019 08:33
Show Gist options
  • Select an option

  • Save Security2431/153b11680a3bbb654a86ee024cf8f038 to your computer and use it in GitHub Desktop.

Select an option

Save Security2431/153b11680a3bbb654a86ee024cf8f038 to your computer and use it in GitHub Desktop.

Revisions

  1. @jeffposnick jeffposnick revised this gist Nov 5, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions register-sw.js
    Original file line number Diff line number Diff line change
    @@ -9,8 +9,8 @@ if ('serviceWorker' in navigator) {
    } else {
    // updatefound is also fired for the very first install. ¯\_(ツ)_/¯
    registration.addEventListener('updatefound', () => {
    registration.installing.addEventListener('statechange', () => {
    if (updatedSW.state === 'installed') {
    registration.installing.addEventListener('statechange', (event) => {
    if (event.target.state === 'installed') {
    if (registration.active) {
    // If there's already an active SW, and skipWaiting() is not
    // called in the SW, then the user needs to close all their
  2. @jeffposnick jeffposnick revised this gist Oct 24, 2018. 1 changed file with 11 additions and 13 deletions.
    24 changes: 11 additions & 13 deletions register-sw.js
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,15 @@
    if ('serviceWorker' in navigator) {
    window.addEventListener('load', async function() {
    const registration = await navigator.serviceWorker.register('/service-worker.js');
    // updatefound is also fired for the very first install. ¯\_(ツ)_/¯
    registration.addEventListener('updatefound', () => {
    const updatedSW = registration.installing || registration.waiting;
    if (updatedSW.state === 'waiting') {
    // A bit of an edge case, just in case the new SW has already made
    // it to the waiting state by the time this code runs.
    // Being in the 'waiting' state implies there's already an active
    // SW, so we don't need to explicitly check for that.
    console.log('Please close all tabs get updates.');
    } else {
    updatedSW.addEventListener('statechange', () => {
    if (registration.waiting && registration.active) {
    // The page has been loaded when there's already a waiting and active SW.
    // This would happen if skipWaiting isn't being called, and there are
    // still old tabs open.
    console.log('Please close all tabs to get updates.');
    } else {
    // updatefound is also fired for the very first install. ¯\_(ツ)_/¯
    registration.addEventListener('updatefound', () => {
    registration.installing.addEventListener('statechange', () => {
    if (updatedSW.state === 'installed') {
    if (registration.active) {
    // If there's already an active SW, and skipWaiting() is not
    @@ -26,7 +24,7 @@ if ('serviceWorker' in navigator) {
    }
    }
    });
    }
    });
    });
    }
    });
    }
  3. @jeffposnick jeffposnick created this gist Oct 15, 2018.
    32 changes: 32 additions & 0 deletions register-sw.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    if ('serviceWorker' in navigator) {
    window.addEventListener('load', async function() {
    const registration = await navigator.serviceWorker.register('/service-worker.js');
    // updatefound is also fired for the very first install. ¯\_(ツ)_/¯
    registration.addEventListener('updatefound', () => {
    const updatedSW = registration.installing || registration.waiting;
    if (updatedSW.state === 'waiting') {
    // A bit of an edge case, just in case the new SW has already made
    // it to the waiting state by the time this code runs.
    // Being in the 'waiting' state implies there's already an active
    // SW, so we don't need to explicitly check for that.
    console.log('Please close all tabs get updates.');
    } else {
    updatedSW.addEventListener('statechange', () => {
    if (updatedSW.state === 'installed') {
    if (registration.active) {
    // If there's already an active SW, and skipWaiting() is not
    // called in the SW, then the user needs to close all their
    // tabs before they'll get updates.
    console.log('Please close all tabs to get updates.');
    } else {
    // Otherwise, this newly installed SW will soon become the
    // active SW. Rather than explicitly wait for that to happen,
    // just show the initial "content is cached" message.
    console.log('Content is cached for the first time!');
    }
    }
    });
    }
    });
    });
    }