-
Notifications
You must be signed in to change notification settings - Fork 274
Expand file tree
/
Copy pathsw.js
More file actions
72 lines (65 loc) · 2.22 KB
/
sw.js
File metadata and controls
72 lines (65 loc) · 2.22 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// 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';
const urlsToCache = ['assets/scripts/main.js', 'assets/scripts/Router.js', 'assets/styles/main.css',
'index.html', 'assets/components/RecipeCard.js', 'assets/components/RecipeExpand.js'];
// Once the service worker has been installed, feed it some initial URLs to cache
// Using code from
// https://developers.google.com/web/fundamentals/primers/service-workers#cache_and_return_requests
self.addEventListener('install', function (event) {
console.log("Hello")
event.waitUntil(
caches.open(CACHE_NAME)
.then(function(cache) {
console.log('Loading into Cache');
return cache.addAll(urlsToCache);
}))
/**
* TODO - Part 2 Step 2
* Create a function as outlined above
*/
});
/**
* Once the service worker 'activates', this makes it so clients loaded
* in the same scope do not need to be reloaded before their fetches will
* go through this service worker
*/
self.addEventListener('activate', function (event) {
event.waitUntil(clients.claim());
})
/**
* TODO - Part 2 Step 3
* Create a function as outlined above, it should be one line
*/
// Intercept fetch requests and store them in the cache
// using code from
// https://developers.google.com/web/fundamentals/primers/service-workers#cache_and_return_requests
self.addEventListener('fetch', function (event) {
event.respondWith(
// Intercepting the fetched response
caches.match(event.request)
.then(function(response) {
if (response) {
return response;
}
return fetch(event.request).then(
function(response) {
if(!response || response.status !== 200 || response.type !== 'basic') {
return response;
}
var responseToCache = response.clone();
// Caching the responses
caches.open(CACHE_NAME)
.then(function(cache) {
cache.put(event.request, responseToCache);
});
return response;
}
);
})
);
});
/**
* TODO - Part 2 Step 4
* Create a function as outlined above
*/