-
Notifications
You must be signed in to change notification settings - Fork 222
/
register-service-worker.ts
70 lines (63 loc) · 2.29 KB
/
register-service-worker.ts
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 { PhpWasmError } from '@php-wasm/util';
import type { WebPHPEndpoint } from './web-php-endpoint';
import { responseTo } from '@php-wasm/web-service-worker';
import { Remote } from 'comlink';
/**
* Run this in the main application to register the service worker or
* reload the registered worker if the app expects a different version
* than the currently registered one.
*
* @param {string} scriptUrl The URL of the service worker script.
* @param {string} expectedVersion The expected version of the service worker script. If
* mismatched with the actual version, the service worker
* will be re-registered.
*/
export async function registerServiceWorker<
Client extends Remote<WebPHPEndpoint>
>(phpApi: Client, scope: string, scriptUrl: string) {
const sw = navigator.serviceWorker;
if (!sw) {
/**
* Service workers may only run in secure contexts.
* See https://w3c.github.io/webappsec-secure-contexts/
*/
if (window.isSecureContext) {
throw new PhpWasmError(
'Service workers are not supported in your browser.'
);
} else {
throw new PhpWasmError(
'WordPress Playground uses service workers and may only work on HTTPS and http://localhost/ sites, but the current site is neither.'
);
}
}
const registration = await sw.register(scriptUrl, {
type: 'module',
// Always bypass HTTP cache when fetching the new Service Worker script:
updateViaCache: 'none',
});
// Check if there's a new service worker available and, if so, enqueue
// the update:
await registration.update();
// Proxy the service worker messages to the web worker:
navigator.serviceWorker.addEventListener(
'message',
async function onMessage(event) {
/**
* Ignore events meant for other PHP instances to
* avoid handling the same event twice.
*
* This is important because the service worker posts the
* same message to all application instances across all browser tabs.
*/
if (scope && event.data.scope !== scope) {
return;
}
const args = event.data.args || [];
const method = event.data.method as keyof Client;
const result = await (phpApi[method] as Function)(...args);
event.source!.postMessage(responseTo(event.data.requestId, result));
}
);
sw.startMessages();
}