-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
service worker caching files successfully
- Loading branch information
Showing
2 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
const FILES_TO_CACHE = [ | ||
"/", | ||
"/index.html", | ||
"/styles.css", | ||
"/index.js", | ||
"/db.js", | ||
|
||
"/manifest.webmanifest", | ||
|
||
"/icons/icon-192x192.png", | ||
|
||
"/icons/icon-512x512.png", | ||
]; | ||
|
||
const CACHE_NAME = "static-cache-v2"; | ||
const DATA_CACHE_NAME = "data-cache-v1"; | ||
|
||
// install | ||
self.addEventListener("install", function (evt) { | ||
evt.waitUntil( | ||
caches.open(CACHE_NAME).then((cache) => { | ||
console.log("Your files were pre-cached successfully!"); | ||
return cache.addAll(FILES_TO_CACHE); | ||
}) | ||
); | ||
|
||
self.skipWaiting(); | ||
}); | ||
|
||
self.addEventListener("activate", function (evt) { | ||
evt.waitUntil( | ||
caches.keys().then((keyList) => { | ||
return Promise.all( | ||
keyList.map((key) => { | ||
if (key !== CACHE_NAME && key !== DATA_CACHE_NAME) { | ||
console.log("Removing old cache data", key); | ||
return caches.delete(key); | ||
} | ||
}) | ||
); | ||
}) | ||
); | ||
|
||
self.clients.claim(); | ||
}); | ||
|
||
// fetch | ||
self.addEventListener("fetch", function (evt) { | ||
// cache successful requests to the API | ||
if (evt.request.url.includes("/api/")) { | ||
evt.respondWith( | ||
caches | ||
.open(DATA_CACHE_NAME) | ||
.then((cache) => { | ||
return fetch(evt.request) | ||
.then((response) => { | ||
// If the response was good, clone it and store it in the cache. | ||
if (response.status === 200) { | ||
cache.put(evt.request.url, response.clone()); | ||
} | ||
|
||
return response; | ||
}) | ||
.catch((err) => { | ||
// Network request failed, try to get it from the cache. | ||
return cache.match(evt.request); | ||
}); | ||
}) | ||
.catch((err) => console.log(err)) | ||
); | ||
|
||
return; | ||
} | ||
|
||
// if the request is not for the API, serve static assets using "offline-first" approach. | ||
// see https://developers.google.com/web/fundamentals/instant-and-offline/offline-cookbook#cache-falling-back-to-network | ||
evt.respondWith( | ||
caches.match(evt.request).then(function (response) { | ||
return response || fetch(evt.request); | ||
}) | ||
); | ||
}); |