forked from nullstack/nullstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
70 lines (56 loc) · 1.39 KB
/
worker.js
File metadata and controls
70 lines (56 loc) · 1.39 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
import client from './client';
import environment from './environment';
import router from './router';
const worker = { ...window.worker };
delete window.worker;
const emptyQueue = Object.freeze([]);
const queuesProxyHandler = {
set(target, name, value) {
target[name] = value;
client.update();
return true;
},
get(target, name) {
return target[name] || emptyQueue;
}
}
worker.queues = new Proxy({}, queuesProxyHandler);
const workerProxyHandler = {
set(target, name, value) {
if (target[name] !== value) {
target[name] = value;
client.update();
}
return true;
}
}
const proxy = new Proxy(worker, workerProxyHandler);
if (worker.enabled) {
window.addEventListener('beforeinstallprompt', function (event) {
event.preventDefault();
proxy.installation = event;
});
async function register() {
if ('serviceWorker' in navigator) {
const request = `/service-worker.js`;
try {
proxy.registration = await navigator.serviceWorker.register(request, { scope: '/' });
} catch (error) {
console.log(error);
};
}
};
register();
}
window.addEventListener('online', () => {
proxy.online = true;
if (environment.mode === 'ssg') {
router._update(router.url);
} else {
proxy.responsive = true;
}
});
window.addEventListener('offline', () => {
proxy.online = false;
});
export default proxy;