-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
34 lines (24 loc) · 865 Bytes
/
index.js
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
/** @param {MessageEvent} messageEvent */
function getServiceWorkerMessage(messageEvent) {
console.log("Received Message From Worker:", messageEvent.data);
}
const workerPath = "./worker.js";
const { serviceWorker } = window.navigator;
serviceWorker.register(workerPath, {})
.then(result => {
const worker = result.active;
worker.postMessage('{"name": "worker"}');
// Uma requisição aleatória para demonstração do cache do Service Worker
fetch('https://jsonplaceholder.typicode.com/posts')
.then(response => response.json())
.then(json => console.log(json))
})
.catch(error => console.log(error));
serviceWorker
.getRegistrations()
.then(registrations => {
console.log('registrations:', registrations)
registrations.forEach(registration => {
registration.unregister()
})
})