Skip to content

Instantly share code, notes, and snippets.

@gokulkrishh
Forked from adactio/basicServiceWorker.js
Created January 14, 2016 03:57

Revisions

  1. @adactio adactio revised this gist Nov 20, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions basicServiceWorker.js
    Original file line number Diff line number Diff line change
    @@ -50,7 +50,7 @@
    // Always fetch non-GET requests from the network
    if (request.method !== 'GET') {
    event.respondWith(
    fetch(request, { credentials: 'include' })
    fetch(request)
    .catch(function () {
    return caches.match('/offline.html');
    })
    @@ -61,7 +61,7 @@
    // For HTML requests, try the network first, fall back to the cache, finally the offline page
    if (request.headers.get('Accept').indexOf('text/html') !== -1) {
    event.respondWith(
    fetch(request, { credentials: 'include' })
    fetch(request)
    .then(function (response) {
    // Stash a copy of this page in the cache
    var copy = response.clone();
  2. @adactio adactio revised this gist Nov 15, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion basicServiceWorker.js
    Original file line number Diff line number Diff line change
    @@ -32,7 +32,7 @@
    event.waitUntil(
    caches.keys()
    .then(function (keys) {
    // Remove caches whose name in no longer valid
    // Remove caches whose name is no longer valid
    return Promise.all(keys
    .filter(function (key) {
    return key.indexOf(version) !== 0;
  3. @adactio adactio revised this gist Nov 15, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion basicServiceWorker.js
    Original file line number Diff line number Diff line change
    @@ -32,7 +32,7 @@
    event.waitUntil(
    caches.keys()
    .then(function (keys) {
    // Remove caches who's name in no longer valid
    // Remove caches whose name in no longer valid
    return Promise.all(keys
    .filter(function (key) {
    return key.indexOf(version) !== 0;
  4. @adactio adactio revised this gist Nov 8, 2015. 1 changed file with 11 additions and 11 deletions.
    22 changes: 11 additions & 11 deletions basicServiceWorker.js
    Original file line number Diff line number Diff line change
    @@ -31,17 +31,17 @@
    self.addEventListener('activate', function (event) {
    event.waitUntil(
    caches.keys()
    .then(function (keys) {
    // Remove caches who's name in no longer valid
    return Promise.all(keys
    .filter(function (key) {
    return key.indexOf(version) !== 0;
    })
    .map(function (key) {
    return caches.delete(key);
    })
    );
    })
    .then(function (keys) {
    // Remove caches who's name in no longer valid
    return Promise.all(keys
    .filter(function (key) {
    return key.indexOf(version) !== 0;
    })
    .map(function (key) {
    return caches.delete(key);
    })
    );
    })
    );
    });

  5. @adactio adactio revised this gist Nov 7, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions basicServiceWorker.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    'use strict';

    # Licensed under a CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
    # http://creativecommons.org/publicdomain/zero/1.0/
    // Licensed under a CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
    // http://creativecommons.org/publicdomain/zero/1.0/

    (function() {

  6. @adactio adactio revised this gist Nov 7, 2015. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions basicServiceWorker.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,8 @@
    'use strict';

    # Licensed under a CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
    # http://creativecommons.org/publicdomain/zero/1.0/

    (function() {

    // Update 'version' if you need to refresh the cache
  7. @adactio adactio revised this gist Nov 7, 2015. 1 changed file with 0 additions and 3 deletions.
    3 changes: 0 additions & 3 deletions basicServiceWorker.js
    Original file line number Diff line number Diff line change
    @@ -83,9 +83,6 @@
    caches.match(request)
    .then(function (response) {
    return response || fetch(request)
    .then(function (response) {
    return response;
    })
    .catch(function () {
    // If the request is for an image, show an offline placeholder
    if (request.headers.get('Accept').indexOf('image') !== -1) {
  8. @adactio adactio revised this gist Nov 7, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions basicServiceWorker.js
    Original file line number Diff line number Diff line change
    @@ -60,6 +60,7 @@
    event.respondWith(
    fetch(request, { credentials: 'include' })
    .then(function (response) {
    // Stash a copy of this page in the cache
    var copy = response.clone();
    caches.open(version + staticCacheName)
    .then(function (cache) {
  9. @adactio adactio revised this gist Nov 7, 2015. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion basicServiceWorker.js
    Original file line number Diff line number Diff line change
    @@ -60,9 +60,10 @@
    event.respondWith(
    fetch(request, { credentials: 'include' })
    .then(function (response) {
    var copy = response.clone();
    caches.open(version + staticCacheName)
    .then(function (cache) {
    cache.put(request, response.clone());
    cache.put(request, copy);
    });
    return response;
    })
  10. @adactio adactio revised this gist Nov 7, 2015. 1 changed file with 10 additions and 1 deletion.
    11 changes: 10 additions & 1 deletion basicServiceWorker.js
    Original file line number Diff line number Diff line change
    @@ -80,7 +80,16 @@
    event.respondWith(
    caches.match(request)
    .then(function (response) {
    return response || fetch(request);
    return response || fetch(request)
    .then(function (response) {
    return response;
    })
    .catch(function () {
    // If the request is for an image, show an offline placeholder
    if (request.headers.get('Accept').indexOf('image') !== -1) {
    return new Response('<svg width="400" height="300" role="img" aria-labelledby="offline-title" viewBox="0 0 400 300" xmlns="http://www.w3.org/2000/svg"><title id="offline-title">Offline</title><g fill="none" fill-rule="evenodd"><path fill="#D8D8D8" d="M0 0h400v300H0z"/><text fill="#9B9B9B" font-family="Helvetica Neue,Arial,Helvetica,sans-serif" font-size="72" font-weight="bold"><tspan x="93" y="172">offline</tspan></text></g></svg>', { headers: { 'Content-Type': 'image/svg+xml' }});
    }
    });
    })
    );
    });
  11. @adactio adactio revised this gist Nov 6, 2015. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions basicServiceWorker.js
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@
    '/path/to/someimage.png',
    '/path/to/someotherimage.png',
    '/',
    '/offline'
    '/offline.html'
    ]);
    });
    };
    @@ -49,7 +49,7 @@
    event.respondWith(
    fetch(request, { credentials: 'include' })
    .catch(function () {
    return caches.match('/offline');
    return caches.match('/offline.html');
    })
    );
    return;
    @@ -69,7 +69,7 @@
    .catch(function () {
    return caches.match(request)
    .then(function (response) {
    return response || caches.match('/offline');
    return response || caches.match('/offline.html');
    })
    })
    );
  12. @adactio adactio revised this gist Nov 6, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion basicServiceWorker.js
    Original file line number Diff line number Diff line change
    @@ -55,7 +55,7 @@
    return;
    }

    // For HTML requests, try the network first, fall back to the the cache, or finally the offline page
    // For HTML requests, try the network first, fall back to the cache, finally the offline page
    if (request.headers.get('Accept').indexOf('text/html') !== -1) {
    event.respondWith(
    fetch(request, { credentials: 'include' })
  13. @adactio adactio created this gist Nov 6, 2015.
    88 changes: 88 additions & 0 deletions basicServiceWorker.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,88 @@
    'use strict';

    (function() {

    // Update 'version' if you need to refresh the cache
    var staticCacheName = 'static';
    var version = 'v1::';

    // Store core files in a cache (including a page to display when offline)
    function updateStaticCache() {
    return caches.open(version + staticCacheName)
    .then(function (cache) {
    return cache.addAll([
    '/path/to/javascript.js',
    '/path/to/stylesheet.css',
    '/path/to/someimage.png',
    '/path/to/someotherimage.png',
    '/',
    '/offline'
    ]);
    });
    };

    self.addEventListener('install', function (event) {
    event.waitUntil(updateStaticCache());
    });

    self.addEventListener('activate', function (event) {
    event.waitUntil(
    caches.keys()
    .then(function (keys) {
    // Remove caches who's name in no longer valid
    return Promise.all(keys
    .filter(function (key) {
    return key.indexOf(version) !== 0;
    })
    .map(function (key) {
    return caches.delete(key);
    })
    );
    })
    );
    });

    self.addEventListener('fetch', function (event) {
    var request = event.request;
    // Always fetch non-GET requests from the network
    if (request.method !== 'GET') {
    event.respondWith(
    fetch(request, { credentials: 'include' })
    .catch(function () {
    return caches.match('/offline');
    })
    );
    return;
    }

    // For HTML requests, try the network first, fall back to the the cache, or finally the offline page
    if (request.headers.get('Accept').indexOf('text/html') !== -1) {
    event.respondWith(
    fetch(request, { credentials: 'include' })
    .then(function (response) {
    caches.open(version + staticCacheName)
    .then(function (cache) {
    cache.put(request, response.clone());
    });
    return response;
    })
    .catch(function () {
    return caches.match(request)
    .then(function (response) {
    return response || caches.match('/offline');
    })
    })
    );
    return;
    }

    // For non-HTML requests, look in the cache first, fall back to the network
    event.respondWith(
    caches.match(request)
    .then(function (response) {
    return response || fetch(request);
    })
    );
    });

    })();