Created
January 22, 2021 16:03
-
-
Save akella/b8a5a90be1b5938d893ea476b341179f to your computer and use it in GitHub Desktop.
sw boilerplate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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