-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsw.js
More file actions
89 lines (84 loc) · 2.57 KB
/
sw.js
File metadata and controls
89 lines (84 loc) · 2.57 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const CACHE_NAME = 'driverhub-v5-static-2';
const STATIC_ASSETS = [
'/',
'/index.html',
'/display',
'/display/laptop',
'/audio',
'/network',
'/chipset',
'/global/tailwind.css',
'/global/search.js',
'/global/driverLoader.js',
'/global/favorites.js',
'/feeds/drivers.json',
'/feeds/drivers-delta.json',
'/feeds/trust-metrics.json',
'/feeds/audio-drivers.json',
'/feeds/network-drivers.json'
];
self.addEventListener('install', function(event) {
event.waitUntil(
caches.open(CACHE_NAME).then(function(cache) {
return cache.addAll(STATIC_ASSETS);
})
);
self.skipWaiting();
});
self.addEventListener('activate', function(event) {
event.waitUntil(
caches.keys().then(function(keys) {
return Promise.all(
keys.filter(function(key) { return key !== CACHE_NAME; }).map(function(key) {
return caches.delete(key);
})
);
})
);
self.clients.claim();
});
self.addEventListener('fetch', function(event) {
if (event.request.method !== 'GET') return;
const requestUrl = new URL(event.request.url);
const isPageOrFeed = event.request.mode === 'navigate'
|| requestUrl.pathname.endsWith('.html')
|| requestUrl.pathname.startsWith('/feeds/');
if (isPageOrFeed) {
event.respondWith(
fetch(event.request).then(function(networkResponse) {
if (networkResponse && networkResponse.ok && requestUrl.origin === self.location.origin) {
const responseClone = networkResponse.clone();
caches.open(CACHE_NAME).then(function(cache) {
cache.put(event.request, responseClone);
});
}
return networkResponse;
}).catch(function() {
return caches.match(event.request).then(function(cachedResponse) {
if (cachedResponse) return cachedResponse;
if (event.request.mode === 'navigate') {
return caches.match('/index.html');
}
return Response.error();
});
})
);
return;
}
event.respondWith(
caches.match(event.request).then(function(cachedResponse) {
if (cachedResponse) return cachedResponse;
return fetch(event.request).then(function(networkResponse) {
if (networkResponse && networkResponse.ok && requestUrl.origin === self.location.origin) {
const responseClone = networkResponse.clone();
caches.open(CACHE_NAME).then(function(cache) {
cache.put(event.request, responseClone);
});
}
return networkResponse;
}).catch(function() {
return cachedResponse;
});
})
);
});