-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
106 lines (91 loc) · 2.76 KB
/
sw.js
File metadata and controls
106 lines (91 loc) · 2.76 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const CACHE_NAME = 'CAP-cache';
// Add whichever assets you want to pre-cache here:
const PRECACHE_ASSETS = [
'/about/index.html',
'/about/faq/index.html',
'/data/index.html',
'/css/homepage.css',
'/css/main.css',
'/css/manual.css',
'/css/map.css',
'/js/common-nonmap.js',
'/js/datasets-common.js',
'/js/manual.js',
'/js/parallax.js',
'/js/settings-common.js',
'/js/ui-common.js',
'/landownership/index.html',
'/landownership/datsets.js',
'/landownership/settings.js',
'/landownership/ui.js',
'/landuse/index.html',
'/landuse/datsets.js',
'/landuse/ui.js',
'/legacy/index.html',
'/manual/index.html',
'/manual/index.md',
'/privacy/index.html',
'/tiles/partial-style_oszoom_names.json',
'/tiles/style_dark_nobuild.json',
'/tiles/style_google_nobuild.json',
'/tiles/style_greyscale_nobuild.json',
'/tiles/style_opencyclemap.json',
'/tiles/style_satellite.json',
'/transport/index.html',
'/transport/datsets.js',
'/transport/settings.js',
'/transport/ui.js',
'/transport/style.css'
]
// Listener for the install event - pre-caches our assets list on service worker install.
self.addEventListener('install', event => {
event.waitUntil((async () => {
const cache = await caches.open(CACHE_NAME);
cache.addAll(PRECACHE_ASSETS);
})());
});
self.addEventListener('activate', event => {
event.waitUntil(self.clients.claim());
});
// This is the service worker with the combined offline experience (Offline page + Offline copy of pages)
const CACHE = "pwabuilder-offline-page";
importScripts('https://storage.googleapis.com/workbox-cdn/releases/5.1.2/workbox-sw.js');
const offlineFallbackPage = "offline.html";
self.addEventListener("message", (event) => {
if (event.data && event.data.type === "SKIP_WAITING") {
self.skipWaiting();
}
});
self.addEventListener('install', async (event) => {
event.waitUntil(
caches.open(CACHE)
.then((cache) => cache.add(offlineFallbackPage))
);
});
if (workbox.navigationPreload.isSupported()) {
workbox.navigationPreload.enable();
}
workbox.routing.registerRoute(
new RegExp('/*'),
new workbox.strategies.StaleWhileRevalidate({
cacheName: CACHE
})
);
self.addEventListener('fetch', (event) => {
if (event.request.mode === 'navigate') {
event.respondWith((async () => {
try {
const preloadResp = await event.preloadResponse;
if (preloadResp) {
return preloadResp;
}
const networkResp = await fetch(event.request);
return networkResp;
} catch (error) {
const cache = await caches.open(CACHE);
const cachedResp = await cache.match(offlineFallbackPage);
return cachedResp;
}
})());
}
});