diff --git a/_includes/footer.html b/_includes/footer.html index 662bf9b7f0..b61cd26e9c 100644 --- a/_includes/footer.html +++ b/_includes/footer.html @@ -12,3 +12,8 @@ anchors.options.visible = 'always'; anchors.add('article h2, article h3, article h4, article h5, article h6'); {% endif %} + diff --git a/sw.js b/sw.js new file mode 100644 index 0000000000..d6d0b7b9ee --- /dev/null +++ b/sw.js @@ -0,0 +1,54 @@ +--- +layout: null +--- +var CACHE_NAME = "pixyll2-{{site.time | date: '%Y%m%d%H%M%S'}}"; + +self.addEventListener("install", function(e) { + e.waitUntil( + caches.open(CACHE_NAME).then(function(cache) { + return cache.addAll([ + "{{ '/css/pixyll.css' | relative_url }}?{{ site.time | date: '%Y%m%d%H%M' }}", + "{{ '/' | relative_url }}" + ]); + }) + ); +}); + +self.addEventListener("activate", function(e) { + e.waitUntil( + caches.keys().then(function(names) { + return Promise.all( + names.map(function(name) { + if (name != CACHE_NAME) { + return caches.delete(name); + } + }) + ); + }) + ); + return clients.claim(); +}); + +addEventListener("fetch", function(e) { + e.respondWith( + caches.match(e.request).then(function(response) { + return response || fetch(e.request).then(function(response) { + var clonedResponse = response.clone(); + var hosts = [ + "https://fonts.googleapis.com", + "https://fonts.gstatic.com", + "https://maxcdn.bootstrapcdn.com", + "https://cdnjs.cloudflare.com" + ]; + hosts.map(function(host) { + if (e.request.url.indexOf(host) === 0) { + caches.open(CACHE_NAME).then(function(cache) { + cache.put(e.request, clonedResponse); + }); + } + }); + return response; + }); + }) + ); +});