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/static/sw.js b/assets/sw.js similarity index 79% rename from static/sw.js rename to assets/sw.js index d42b04c..d7d517a 100644 --- a/static/sw.js +++ b/assets/sw.js @@ -1,11 +1,21 @@ +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 cacheName = self.location.pathname const request = event.request; - if (request.method !== "GET") { return; }