-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathsnippets.txt
72 lines (64 loc) · 2.26 KB
/
snippets.txt
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
// This file contains code snippets we may use during the workshop
// CSS Safe Area
.container {
margin: env(safe-area-inset-top)
env(safe-area-inset-right)
env(safe-area-inset-bottom)
env(safe-area-inset-left) !important;
}
// Service Worker Installation
self.addEventListener("install", event => {
// install the assets of my PWA
event.waitUntil(
caches.open("assets").then(cache => {
return cache.addAll(assets);
})
);
// self.skipWaiting(); // activate the service worker immediately
});
// Cache first strategy
self.addEventListener("fetch", event => {
event.respondWith(
caches.match(event.request) // searching in the cache
.then( response => {
if (response) {
// The request is in the cache
return response; // cache hit
} else {
// We need to go to the network
return fetch(event.request); // cache miss
}
})
);
});
// Network first strategy
self.addEventListener('fetch', event => {
event.respondWith(
fetch(event.request) // I go to the network ALWAYS
.catch( error => { // if the network is down, I go to the cache
return caches.open("assets")
.then( cache => {
return cache.match(request);
});
})
);
});
// State while revalidate strategy
self.addEventListener('fetch', event => {
event.respondWith(
caches.match(event.request)
.then( response => {
// Even if the response is in the cache, we fetch it
// and update the cache for future usage
const fetchPromise = fetch(event.request).then(
networkResponse => {
caches.open("assets").then( cache => {
cache.put(event.request, networkResponse.clone());
return networkResponse;
});
});
// We use the currently cached version if it's there
return response || fetchPromise; // cached or a network fetch
})
);
});