Skip to content

Instantly share code, notes, and snippets.

@akella
Created January 22, 2021 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akella/b8a5a90be1b5938d893ea476b341179f to your computer and use it in GitHub Desktop.
Save akella/b8a5a90be1b5938d893ea476b341179f to your computer and use it in GitHub Desktop.
sw boilerplate
// navigator.serviceWorker.register('sw.js');
var CACHE = "cache-name";
self.addEventListener("install", function (evt) {
console.log("Установка");
evt.waitUntil(precache());
});
self.addEventListener("fetch", function (e) {
console.log("[ServiceWorker] Запрос", e.request.url);
// проверяем кеш
e.respondWith(
caches
.match(e.request)
.then(function (response) {
// если это есть в кеше
if (response) {
console.log(
"[ServiceWorker] Found in Cache",
e.request.url,
response
);
// версия из кеша
return response;
}
// сетевой запрос, как фоллбек
return fetch(e.request)
})
);
});
function precache() {
return caches.open(CACHE).then(function (cache) {
return cache.addAll(["./style.css"]);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment