Generate pages list in sw.js

This commit is contained in:
Alex Shpak 2020-04-21 22:35:30 +02:00
parent 9c5fc12dd8
commit c672d64f12
2 changed files with 14 additions and 3 deletions

View file

@ -1,6 +1,7 @@
{{ $swJS := resources.Get "sw.js" | resources.ExecuteAsTemplate "sw.js" . }}
if (navigator.serviceWorker) { if (navigator.serviceWorker) {
navigator.serviceWorker.register( navigator.serviceWorker.register(
"{{ "/sw.js" | relURL }}", "{{ $swJS.RelPermalink }}",
{ scope: "{{ "/" | relURL }}" } { scope: "{{ "/" | relURL }}" }
); );
} }

View file

@ -1,11 +1,21 @@
const cacheName = self.location.pathname
const pages = [
{{ range .Site.AllPages -}}
"{{ .RelPermalink }}",
{{ end }}
];
self.addEventListener("install", function (event) { self.addEventListener("install", function (event) {
self.skipWaiting(); self.skipWaiting();
const precache = caches.open(cacheName).then((cache) => {
return cache.addAll(pages);
});
event.waitUntil(precache);
}); });
self.addEventListener("fetch", (event) => { self.addEventListener("fetch", (event) => {
const cacheName = self.location.pathname
const request = event.request; const request = event.request;
if (request.method !== "GET") { if (request.method !== "GET") {
return; return;
} }