-
Notifications
You must be signed in to change notification settings - Fork 2
/
worker.js
140 lines (125 loc) · 3.5 KB
/
worker.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
var staticAssetsCacheName = 'StaticAssets',
imageAssetsCacheName = 'ImgStatic',
version = 'v2.34::';
var staticPrimaryAssets = [
'/',
'/styles.css',
'/offline.html'
];
var staticSecondaryAssets = [
'/assets/js/all.js',
'/assets/css/all.css'
];
self.addEventListener('install', event => {
console.log(' 💾 Worker Installation');
self.skipWaiting();
event.waitUntil(updateStaticAssets());
});
self.addEventListener('activate', event => {
console.log(' ☑ Worker Activation');
event.waitUntil(
caches.keys().
then(function(keys) {
// Remove caches no longer valid
return Promise.all(keys.
filter(function(key) {
return key.indexOf(version) !== 0;
}).
map(function(key) {
console.log(' New key available, delete old key: ' + key);
return caches.delete(key);
})
);
}).
catch(function() {
console.log('Something Went wrong');
})
);
});
self.addEventListener('fetch', event => {
// … Perhaps respond to this fetch in a useful way?
var request = event.request;
if (isDoNotCacheDomain(request)) {
event.respondWith(fetch(event.request));
} else if (isHTMLContent(request)) {
event.respondWith(handleHTMLContent(request));
} else if (isStaticImage(request)) {
event.respondWith(handleImageContent(request));
} else {
event.respondWith(
caches.match(event.request).then(function(response) {
return response || fetch(event.request);
})
);
}
});
function handleImageContent(request) {
return caches.open(imageAssetsCacheName).then(function(cache) {
return cache.match(request.url).then(function(response) {
if (response) return response;
return fetch(request).then(function(networkResponse) {
cache.put(request.url, networkResponse.clone());
return networkResponse;
});
});
});
}
function handleHTMLContent(request) {
// Fix for Chrome bug: https://code.google.com/p/chromium/issues/detail?id=573937
if (request.mode != 'navigate') {
request = new Request(request.url, {
method: 'GET',
headers: request.headers,
mode: request.mode,
credentials: request.credentials,
redirect: request.redirect,
});
}
return fetch(request).
then(function(response) {
// Stash a copy of this page in the cache
var copy = response.clone();
caches.open(version + staticAssetsCacheName).
then(function(cache) {
cache.put(request, copy);
});
return response;
}).
catch(function() {
return caches.match(request).
then(function(response) {
return response || caches.match('/offline.html');
});
});
}
function updateStaticAssets() {
return caches.open(version + staticAssetsCacheName).
then(function(cache) {
cache.addAll(staticSecondaryAssets);
return cache.addAll(staticPrimaryAssets);
});
}
function isStaticImage(request) {
if (request.headers.get('Accept').indexOf('image') !== -1) {
return true;
}
return false;
}
function isHTMLContent(request) {
if (request.headers.get('Accept').indexOf('text/html') !== -1) {
return true;
}
return false;
}
function isDoNotCacheDomain(request) {
if (request.url.indexOf('analytics') !== -1) {
return true;
}
return false;
}
if ('serviceWorker' in navigator) {
console.log(' ☑ Service Worker Is available. Yeah!');
navigator.serviceWorker.register('/worker.js?v=1', {
scope: '/',
});
}