-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
113 lines (107 loc) · 3.19 KB
/
sw.js
File metadata and controls
113 lines (107 loc) · 3.19 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
106
107
108
109
110
111
112
113
const CACHE_NAME = 'ednalite-v32';
const PRECACHE_ASSETS = [
'./',
'./index.html',
'./css/style.css',
'./js/app.js',
'./js/db.js',
'./js/ui.js',
'./js/gps.js',
'./js/scanner.js',
'./js/export.js',
'./js/gps-pill.js',
'./js/photo.js',
'./js/util.js',
'./js/templates.js',
'./js/views/home.js',
'./js/views/project-form.js',
'./js/views/project-dashboard.js',
'./js/views/sample-entry.js',
'./js/views/sample-detail.js',
'./js/views/export-dialog.js',
'./js/views/more-modal.js',
'./js/views/offline-guide.js',
'./js/views/about.js',
'./js/views/settings.js',
'./js/views/changelog.js',
'./lib/idb.js',
'./lib/html5-qrcode.min.js',
'./lib/papaparse.min.js',
'./lib/sql-wasm.js',
'./lib/sql-wasm.wasm',
'./manifest.json',
'./icons/icon-192.png',
'./icons/icon-512.png',
'./icons/apple-touch-icon.png',
];
/**
* Handles the service worker install event.
*
* Opens the versioned cache and precaches all assets listed in PRECACHE_ASSETS.
* The install is held pending until all assets are cached (waitUntil).
*
* @param {ExtendableEvent} event - The install lifecycle event.
* @returns {void}
*/
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => cache.addAll(PRECACHE_ASSETS)).then(() => self.skipWaiting())
);
});
/**
* Handles the service worker activate event.
*
* Deletes all caches whose name does not match CACHE_NAME to remove
* stale assets from previous versions.
*
* @param {ExtendableEvent} event - The activate lifecycle event.
* @returns {void}
*/
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((keyList) =>
Promise.all(
keyList
.filter((key) => key !== CACHE_NAME)
.map((key) => caches.delete(key))
)
)
);
self.clients.claim();
});
/**
* Handles fetch events using a cache-first strategy.
*
* Checks the cache for a matching response. On a cache hit the cached
* response is returned immediately. On a cache miss the request is fetched
* from the network, the response clone is stored in the cache for future
* requests, and the live response is returned. If both the cache and network
* are unavailable the browser's default error handling takes over.
*
* Only GET requests are intercepted; non-GET requests are passed through
* to the network without caching.
*
* @param {FetchEvent} event - The fetch lifecycle event.
* @returns {void}
*/
self.addEventListener('fetch', (event) => {
if (event.request.method !== 'GET') return;
event.respondWith(
caches.open(CACHE_NAME).then(async (cache) => {
const cached = await cache.match(event.request);
if (cached) return cached;
try {
const response = await fetch(event.request);
if (response && response.status === 200) {
cache.put(event.request, response.clone());
}
return response;
} catch {
// Network unavailable and asset not in cache.
// All critical assets are precached, so this path should not be hit
// during normal offline operation.
return new Response('Offline', { status: 503, statusText: 'Service Unavailable' });
}
})
);
});