blob: f670e3b014ae6fc9879739811fb697f912ab7680 (
plain)
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
|
<script>
async function registerWorker() {
const sw = navigator.serviceWorker;
const swr = await sw.register("service-worker.sjs", { scope: "" });
dump(`Registered, scope is: ${swr.scope}\n`);
await new Promise(resolve => {
const worker = swr.installing || swr.waiting || swr.active;
if (worker.state === 'activated') {
return resolve();
}
worker.addEventListener('statechange', () => {
if (worker.state === 'activated') {
return resolve();
}
});
});
}
async function unregisterWorker() {
const sw = navigator.serviceWorker;
const swr = await sw.register("service-worker.sjs", { scope: "" });
return swr.unregister();
}
async function fetchFromWorker() {
const response = await fetch("whatever");
const text = await response.text();
console.log(`Response ${text}`);
}
async function registerAndFetchFromWorker() {
await registerWorker();
await fetchFromWorker();
}
fetchFromWorker();
</script>
|