-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.boot.html
140 lines (119 loc) · 3.75 KB
/
index.boot.html
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>service bootstrap</title>
</head>
<style>
body {
background: #1e1e1e; margin: 1px; color: #ccc; font-family: sans-serif;
}
.hidden { display: none; }
.w3-border{border:1px solid #ccc!important}
.w3-grey,.w3-hover-grey:hover,.w3-gray,.w3-hover-gray:hover{
color:#000!important;background-color:#9e9e9e!important
}
.messages{ position: absolute; top:3em; left: 2em; }
</style>
<body>
<div class="w3-border container hidden">
<div id="progress-bar" class="w3-grey" style="height:24px;width:0%"></div>
</div>
<div class="messages hidden"></div>
</body>
<script src="/shared/vendor/json5v-2.0.0.min.js"></script>
<script>
const container = document.querySelector('.container');
const messages = document.querySelector('.messages');
const progressBar = document.getElementById('progress-bar');
const delay = ms => new Promise(res => setTimeout(res, ms));
const noLoad = (reason="unknown reason") => {
//TODO: maybe test progress bar here somehow
messages.textContent = 'bootstrap will not load: ' + reason;
messages.classList.remove('hidden')
};
async function getServiceWorker() {
//should ever use navigator.serviceWorker.ready?
let reg, error;
if (!('serviceWorker' in navigator)) {
error = 'no service worker support';
return console.log('Service worker support is required and not present. Will not continue.');
}
try {
reg = await navigator.serviceWorker.register('index.sw.js', { scope: '/' });
reg && console.log('Service worker registration succeeded. Scope is ' + reg.scope);
} catch (e) {
error = e;
error && console.log('Service worker registration failed with ' + error);
}
if (!error && !!reg) {
let count = 0;
const MAX_COUNT = 10;
while (!reg.active && count < MAX_COUNT) {
await delay(300);
count++;
}
if (count >= MAX_COUNT) {
return console.error('gave up waiting for service worker registration to activate');
}
}
return reg && reg.active;
}
const serviceWorkerMessageHandler = (manifest) => {
const totalModules = manifest.modules.length;
let doneModules=0;
return ({ data = {} }) => {
const { msg, modules, module } = data;
if(module){
//module.route && console.log(module.route);
doneModules++;
progressBar.style.width = `${100 * doneModules / totalModules}%`;
// TODO: if this page has a parent, then update progress to it
// include filesize estimate?
return;
}
console.log(msg);
//console.log({ manifest });
//TODO: modules returned should match the manifest
if (!modules) {
console.log('bootstrap fail: modules unavailable');
return;
}
localStorage.setItem('moduleCache', JSON.stringify(
modules
.filter(x => x && x.type && x.type === 'domComponent')
.map(x => x.route)
));
};
};
(async function () {
if(document.location.href.includes('::preview::')) return noLoad('no boot in preview');
container.classList.remove('hidden');
const serviceWorker = await getServiceWorker();
if (!serviceWorker) {
console.error('bootstrap fail: could not find service worker');
return;
}
const manifestUrl = 'service.manifest.json';
let manifestResponseText, manifest;
try {
const manifestRes = await fetch(manifestUrl, { cache: "no-store" });
manifestResponseText = await manifestRes.text();
manifest = JSON5.parse(manifestResponseText);
} catch(e){
console.error(e);
console.error(manifestResponseText);
}
if (!manifest) {
console.error('bootstrap fail: could not find service manifest');
return;
}
navigator.serviceWorker.addEventListener('message', serviceWorkerMessageHandler(manifest));
serviceWorker.postMessage({
bootstrap: {
manifest: manifestUrl
}
});
})();
</script>
</html>