Skip to content

Instantly share code, notes, and snippets.

@neoFelhz
Last active April 10, 2017 05:02
Show Gist options
  • Save neoFelhz/50800b701d00dc6d874be95da2cd7655 to your computer and use it in GitHub Desktop.
Save neoFelhz/50800b701d00dc6d874be95da2cd7655 to your computer and use it in GitHub Desktop.
Service Workers Cached JS
Display the source blob
Display the rendered blob
Raw
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 22 22">
<defs id="defs3051">
<style type="text/css" id="current-color-scheme">
.ColorScheme-Text {
color:#4d4d4d;
}
</style>
</defs>
<g
transform="translate(-421.71429,-525.79074)">
<path
style="opacity:1;fill:currentColor;fill-opacity:1;stroke:none"
d="M 11 4 A 6 6 0 0 0 5 10 A 6 6 0 0 0 5.0039062 10.128906 A 4 4 0 0 0 2 14 A 4 4 0 0 0 6 18 L 15 18 A 5 5 0 0 0 20 13 A 5 5 0 0 0 16.757812 8.3242188 A 6 6 0 0 0 11 4 z M 11 5 A 5 5 0 0 1 15.919922 9.1113281 A 4.0000019 4.0000019 0 0 1 19 13 A 4.0000019 4.0000019 0 0 1 15 17 L 6 17 A 2.9999979 2.9999979 0 0 1 3 14 A 2.9999979 2.9999979 0 0 1 6 11 A 2.9999979 2.9999979 0 0 1 6.1074219 11.005859 A 5 5 0 0 1 6 10 A 5 5 0 0 1 11 5 z "
transform="translate(421.71429,525.79074)"
class="ColorScheme-Text"
id="path4196" />
<path
style="opacity:1;fill:#bdc3c7;fill-opacity:1;stroke:none"
d="m 442.71429,540.29071 a 4.5,4.5 0 0 1 -4.5,4.5 4.5,4.5 0 0 1 -4.5,-4.5 4.5,4.5 0 0 1 4.5,-4.5 4.5,4.5 0 0 1 4.5,4.5 z"
id="path4306" />
<path
style="opacity:1;fill:#ffffff;fill-opacity:1;stroke-linecap:round"
d="M 14 14 L 14 15 L 15 15 L 15 14 L 14 14 z M 16 14 L 16 15 L 17 15 L 17 14 L 16 14 z M 18 14 L 18 15 L 19 15 L 19 14 L 18 14 z "
transform="translate(421.71429,525.79074)"
id="rect4521" />
</g>
</svg>
'use strict';
const version = 'v201704100047';
const __DEVELOPMENT__ = false;
const __DEBUG__ = false;
const offlineResources = ['/', '/offline.html', '/offline.svg'];
const ignoreFetch = [/https?:\/\/example.disqus.com\//, /chrome-extension:\/\//];
function onInstall(event) {
log('install event in progress.');
event.waitUntil(updateStaticCache());
self.skipWaiting()
}
function updateStaticCache() {
return caches.open(cacheKey('offline')).then((cache) = > {
return cache.addAll(offlineResources)
}).then(() = > {
log('installation complete!')
})
}
function onFetch(event) {
const request = event.request;
if (shouldAlwaysFetch(request)) {
event.respondWith(networkedOrOffline(request));
return
}
if (shouldFetchAndCache(request)) {
event.respondWith(networkedOrCached(request));
return
}
event.respondWith(cachedOrNetworked(request))
}
function networkedOrCached(request) {
return networkedAndCache(request).
catch (() = > {
return cachedOrOffline(request)
})
}
function networkedAndCache(request) {
return fetch(request).then((response) = > {
var copy = response.clone();
caches.open(cacheKey('resources')).then((cache) = > {
cache.put(request, copy)
});
log("(network: cache write)", request.method, request.url);
return response
})
}
function cachedOrNetworked(request) {
return caches.match(request).then((response) = > {
log(response ? '(cached)' : '(network: cache miss)', request.method, request.url);
return response || networkedAndCache(request).
catch (() = > {
return offlineResponse(request)
})
})
}
function networkedOrOffline(request) {
return fetch(request).then((response) = > {
log('(network)', request.method, request.url);
return response
}).
catch (() = > {
return offlineResponse(request)
})
}
function cachedOrOffline(request) {
return caches.match(request).then((response) = > {
return response || offlineResponse(request)
})
}
function offlineResponse(request) {
log('(offline)', request.method, request.url);
if (request.url.match(/\.(jpg|png|gif|svg|jpeg)(\?.*)?$/)) {
return caches.match('/offline.svg')
} else {
return caches.match('/offline.html')
}
}
function onActivate(event) {
log('activate event in progress.');
event.waitUntil(removeOldCache())
}
function removeOldCache() {
return caches.keys().then((keys) = > {
return Promise.all(keys.filter((key) = > {
return !key.startsWith(version)
}).map((key) = > {
return caches.delete(key)
}))
}).then(() = > {
log('removeOldCache completed.')
})
}
function cacheKey() {
return [version, ...arguments].join(':')
}
function log() {
if (developmentMode()) {
console.log("SW:", ...arguments)
}
}
function shouldAlwaysFetch(request) {
return __DEVELOPMENT__ || request.method !== 'GET' || ignoreFetch.some(regex = > request.url.match(regex))
}
function shouldFetchAndCache(request) {
return ~request.headers.get('Accept').indexOf('text/html')
}
function developmentMode() {
return __DEVELOPMENT__ || __DEBUG__
}
log("Hello from ServiceWorker land!", version);
self.addEventListener('install', onInstall);
self.addEventListener('fetch', onFetch);
self.addEventListener("activate", onActivate);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment