From c672d64f126178f0facb91204761b1b913743d71 Mon Sep 17 00:00:00 2001 From: Alex Shpak Date: Tue, 21 Apr 2020 22:35:30 +0200 Subject: Generate pages list in sw.js --- assets/sw-register.js | 3 ++- assets/sw.js | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++ static/sw.js | 44 ----------------------------------------- 3 files changed, 56 insertions(+), 45 deletions(-) create mode 100644 assets/sw.js delete mode 100644 static/sw.js diff --git a/assets/sw-register.js b/assets/sw-register.js index 583532c..0b71f03 100644 --- a/assets/sw-register.js +++ b/assets/sw-register.js @@ -1,6 +1,7 @@ +{{ $swJS := resources.Get "sw.js" | resources.ExecuteAsTemplate "sw.js" . }} if (navigator.serviceWorker) { navigator.serviceWorker.register( - "{{ "/sw.js" | relURL }}", + "{{ $swJS.RelPermalink }}", { scope: "{{ "/" | relURL }}" } ); } diff --git a/assets/sw.js b/assets/sw.js new file mode 100644 index 0000000..d7d517a --- /dev/null +++ b/assets/sw.js @@ -0,0 +1,54 @@ +const cacheName = self.location.pathname +const pages = [ + {{ range .Site.AllPages -}} + "{{ .RelPermalink }}", + {{ end }} +]; + +self.addEventListener("install", function (event) { + self.skipWaiting(); + + const precache = caches.open(cacheName).then((cache) => { + return cache.addAll(pages); + }); + event.waitUntil(precache); +}); + +self.addEventListener("fetch", (event) => { + const request = event.request; + if (request.method !== "GET") { + return; + } + + /** + * @param {Response} response + * @returns {Promise} + */ + function saveToCache(response) { + if (cacheable(response)) { + return caches + .open(cacheName) + .then((cache) => cache.put(request, response.clone())) + .then(() => response); + } else { + return response; + } + } + + /** + * @param {Error} error + */ + function serveFromCache(error) { + return caches.open(cacheName).then((cache) => cache.match(request.url)); + } + + /** + * @param {Response} response + * @returns {Boolean} + */ + function cacheable(response) { + return response.type === "basic" && response.ok && !response.headers.has("Content-Disposition") + } + + event.respondWith(fetch(request).then(saveToCache).catch(serveFromCache)); +}); diff --git a/static/sw.js b/static/sw.js deleted file mode 100644 index d42b04c..0000000 --- a/static/sw.js +++ /dev/null @@ -1,44 +0,0 @@ -self.addEventListener("install", function (event) { - self.skipWaiting(); -}); - -self.addEventListener("fetch", (event) => { - const cacheName = self.location.pathname - const request = event.request; - - if (request.method !== "GET") { - return; - } - - /** - * @param {Response} response - * @returns {Promise} - */ - function saveToCache(response) { - if (cacheable(response)) { - return caches - .open(cacheName) - .then((cache) => cache.put(request, response.clone())) - .then(() => response); - } else { - return response; - } - } - - /** - * @param {Error} error - */ - function serveFromCache(error) { - return caches.open(cacheName).then((cache) => cache.match(request.url)); - } - - /** - * @param {Response} response - * @returns {Boolean} - */ - function cacheable(response) { - return response.type === "basic" && response.ok && !response.headers.has("Content-Disposition") - } - - event.respondWith(fetch(request).then(saveToCache).catch(serveFromCache)); -}); -- cgit v1.2.3