-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserviceworker.js
166 lines (161 loc) · 4.25 KB
/
serviceworker.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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
const assetCacheName = 'assetsv1-01';
addEventListener('install', installEvent => {
skipWaiting();
installEvent.waitUntil(
caches.open(assetCacheName) // cache asset files
.then( assetCache => {
assetCache.addAll([
'/fonts/lato-italic.woff2',
'/fonts/lato-light.woff2',
'/fonts/lato-regular.woff2',
'/fonts/lato-semibold.woff2',
'/i/chevron-left.svg',
'/i/chevron-right.svg',
'/i/home.svg',
'/guideline/01-CG80',
'/guideline/01-CG81',
'/guideline/02-CG71',
'/guideline/02-CG92',
'/guideline/02-CG94',
'/guideline/02-CG95',
'/guideline/02-CG108',
'/guideline/02-CG126',
'/guideline/02-CG127',
'/guideline/02-CG130',
'/guideline/02-CG144',
'/guideline/02-CG147',
'/guideline/02-CG167',
'/guideline/02-CG168',
'/guideline/02-CG172',
'/guideline/02-CG180',
'/guideline/02-CG181',
'/guideline/02-CG187',
'/guideline/03-CG57',
'/guideline/03-CG153',
'/guideline/04-CG134',
'/guideline/05-CG10',
'/guideline/05-CG15',
'/guideline/05-CG87',
'/guideline/06-CG49',
'/guideline/06-CG61',
'/guideline/06-CG84',
'/guideline/06-CG86',
'/guideline/06-CG99',
'/guideline/06-CG106',
'/guideline/06-CG118',
'/guideline/06-CG125',
'/guideline/06-CG131',
'/guideline/06-CG141',
'/guideline/06-CG152',
'/guideline/06-CG165',
'/guideline/06-CG166',
'/guideline/06-CG184',
'/guideline/06-CG188',
'/guideline/07-CG135',
'/guideline/07-CG140',
'/guideline/07-CG161',
'/guideline/08-CG143',
'/guideline/08-CG151',
'/guideline/09-CG124',
'/guideline/10-CG2',
'/guideline/10-CG169',
'/guideline/10-CG182',
'/guideline/10-CG187',
'/guideline/11-CG35',
'/guideline/11-CG68',
'/guideline/11-CG102',
'/guideline/11-CG105',
'/guideline/11-CG109',
'/guideline/11-CG137',
'/guideline/11-CG150',
'/guideline/11-CG162',
'/guideline/11-CG173',
'/guideline/11-CG176',
'/guideline/11-CG186',
'/guideline/12-CG85',
'/guideline/13-CG9',
'/guideline/13-CG31',
'/guideline/13-CG90',
'/guideline/13-CG115',
'/guideline/13-CG178',
'/guideline/14-CG69',
'/guideline/14-CG83',
'/guideline/14-CG101',
'/guideline/14-CG117',
'/guideline/14-CG121',
'/guideline/14-CG163',
'/guideline/14-CG191',
'/guideline/15-CG79',
'/guideline/15-CG177',
'/guideline/16-CG97',
'/guideline/16-CG148',
'/guideline/16-CG171',
'/guideline/16-CG175',
'/about.php',
]); // end addAll
return assetCache.addAll([
'/css/all.css',
'/js/global.js',
'/offline.html',
'/'
]); // end return addAll
}) // end open then
); // end waitUntil
}); // end addEventListener
addEventListener('activate', activateEvent => {
activateEvent.waitUntil(
caches.keys()
.then( cacheNames => {
return Promise.all(
cacheNames.map( cacheName => {
if (cacheName != assetCacheName) {
return caches.delete(cacheName);
} // end if
}) // end map
); // end return Promise.all
}) // end keys then
.then( () => {
return clients.claim();
}) // end then
); // end waitUntil
}); // end addEventListener
addEventListener( 'fetch', fetchEvent => {
const request = fetchEvent.request;
// Try the cache first, then the network
fetchEvent.respondWith(
caches.match( request )
.then( responseFromCache => {
if ( responseFromCache ) {
// Refresh the cache with a fresh version of this file from the network
fetchEvent.waitUntil(
fetch( request )
.then( responseFromFetch => {
caches.open( assetCacheName )
.then( assetCache => {
return assetCache.put( request, responseFromFetch );
})
})
);
return responseFromCache;
}
return fetch( request )
.then( responseFromFetch => {
// Cache any files you fetch
let copy = responseFromFetch.clone();
fetchEvent.waitUntil(
caches.open( assetCacheName )
.then( assetCache => {
return assetCache.put( request, copy );
})
);
return responseFromFetch;
})
.catch( error => {
// As a last resort, show an offline page
if ( request.mode === 'navigate' ) {
return caches.match('/offline.html');
}
})
})
);
});