-
Notifications
You must be signed in to change notification settings - Fork 64
/
SharedService_ServiceWorker.js
28 lines (25 loc) · 1.05 KB
/
SharedService_ServiceWorker.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
"use strict";
// Install the service worker as soon as possible.
globalThis.addEventListener('install', (/** @type {ExtendableEvent} */ event) => {
event.waitUntil(globalThis.skipWaiting());
});
globalThis.addEventListener('activate', (/** @type {ExtendableEvent} */ event) => {
event.waitUntil(globalThis.clients.claim());
});
// Forward messages (and ports) from client to client.
globalThis.addEventListener('message', async event => {
if (event.data?.sharedService) {
const client = await globalThis.clients.get(event.data.clientId);
client.postMessage(event.data, event.ports);
}
});
// Tell clients their clientId. A service worker isn't actually needed
// for a context to get its clientId, but this also doubles as a way
// to verify that the service worker is active.
globalThis.addEventListener('fetch', async (/** @type {FetchEvent} */ event) => {
if (event.request.url === globalThis.registration.scope + 'clientId') {
return event.respondWith(new Response(event.clientId, {
headers: { "Content-Type": "text/plain" }
}));
}
});