Skip to content

Instantly share code, notes, and snippets.

@alvarotrigo
Created January 22, 2021 12:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alvarotrigo/77fa5ab328907a69b3fa3654ca7de99a to your computer and use it in GitHub Desktop.
Save alvarotrigo/77fa5ab328907a69b3fa3654ca7de99a to your computer and use it in GitHub Desktop.
sw.js
'use strict';
// Licensed under a CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
// http://creativecommons.org/publicdomain/zero/1.0/
// https://gist.github.com/adactio/fbaa3a5952774553f5e7
(function() {
// Update 'version' if you need to refresh the cache
var staticCacheName = 'sw-test-cache';
var version = 'v12::';
var urlsToCache = [
'/',
'views/layouts/offline.html',
'dist/scripts-mobile.min.js',
'dist/styles-mobile.min.css',
'imgs/app/ops.png',
];
// Store core files in a cache (including a page to display when offline)
function updateStaticCache() {
console.log("weeee");
return caches.open(version + staticCacheName)
.then(function (cache) {
return cache.addAll(urlsToCache);
});
};
self.addEventListener('install', function (event) {
console.log("installing...");
event.waitUntil(updateStaticCache());
});
self.addEventListener('activate', function (event) {
console.log("activating...");
event.waitUntil(
caches.keys()
.then(function (keys) {
// Remove caches whose name is no longer valid
return Promise.all(keys
.filter(function (key) {
console.log("checking.... " + key);
return key.indexOf(version) !== 0;
})
.map(function (key) {
console.log("deleing.... " + key);
return caches.delete(key);
})
);
})
);
});
self.addEventListener('fetch', function (event) {
console.log("fethcing...");
var request = event.request;
// Always fetch non-GET requests from the network
if (request.method !== 'GET') {
console.log("is not GET");
event.respondWith(
fetch(request)
.catch(function () {
return caches.match('views/layouts/offline.html');
})
);
return;
}
// 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) {
// Fix for Chrome bug: https://code.google.com/p/chromium/issues/detail?id=573937
if (request.mode != 'navigate') {
request = new Request(request.url, {
method: 'GET',
headers: request.headers,
mode: request.mode,
credentials: request.credentials,
redirect: request.redirect
});
}
event.respondWith(
fetch(request)
.then(function (response) {
// Stash a copy of this page in the cache
var copy = response.clone();
console.log("fetching..." + (version + staticCacheName));
caches.open(version + staticCacheName)
.then(function (cache) {
cache.put(request, copy);
});
return response;
})
.catch(function () {
return caches.match(request)
.then(function (response) {
return response || caches.match('views/layouts/offline.html');
})
})
);
return;
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment