-
Notifications
You must be signed in to change notification settings - Fork 559
/
Copy pathsw.js
40 lines (36 loc) · 1.71 KB
/
sw.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// sw.js - This file needs to be in the root of the directory to work,
// so do not move it next to the other scripts
const CACHE_NAME = 'lab-7-starter';
// Installs the service worker. Feed it some initial URLs to cache
self.addEventListener('install', function (event) {
event.waitUntil(
caches.open(CACHE_NAME).then(function (cache) {
// B6. TODO - Add all of the URLs from RECIPE_URLs here so that they are
// added to the cache when the ServiceWorker is installed
return cache.addAll([]);
})
);
});
// Activates the service worker
self.addEventListener('activate', function (event) {
event.waitUntil(self.clients.claim());
});
// Intercept fetch requests and cache them
self.addEventListener('fetch', function (event) {
// We added some known URLs to the cache above, but tracking down every
// subsequent network request URL and adding it manually would be very taxing.
// We will be adding all of the resources not specified in the intiial cache
// list to the cache as they come in.
/*******************************/
// This article from Google will help with this portion. Before asking ANY
// questions about this section, read this article.
// NOTE: In the article's code REPLACE fetch(event.request.url) with
// fetch(event.request)
// https://developer.chrome.com/docs/workbox/caching-strategies-overview/
/*******************************/
// B7. TODO - Respond to the event by opening the cache using the name we gave
// above (CACHE_NAME)
// B8. TODO - If the request is in the cache, return with the cached version.
// Otherwise fetch the resource, add it to the cache, and return
// network response.
});