8000 better ServiceWorker logic · pyscript/polyscript@2e93d2a · GitHub
[go: up one dir, main page]

Skip to content

Commit 2e93d2a

Browse files
committed
better ServiceWorker logic
1 parent 28c3ef9 commit 2e93d2a

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

docs/service-worker.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

esm/service-worker.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,31 @@ import sabayon from 'https://cdn.jsdelivr.net/npm/sabayon/dist/polyfill.js';
22

33
// ignore browsers that already support SharedArrayBuffer
44
if (!globalThis.crossOriginIsolated) {
5+
const { isArray } = Array;
6+
const isOptions = args => args.length && typeof args[0] === 'object' && args[0] !== null;
7+
58
// early patch Blob to inject sabayon polyfill for service-worker
69
globalThis.Blob = class extends Blob {
7-
constructor(blobParts, options) {
8-
if (options.type === 'text/javascript' && typeof blobParts.at(0) === 'string') {
10+
constructor(blobParts, ...args) {
11+
if (
12+
isOptions(args) &&
13+
args[0].type === 'text/javascript' &&
14+
isArray(blobParts) &&
15+
typeof blobParts.at(0) === 'string'
16+
) {
917
blobParts[0] = blobParts[0].replace(
1018
/^\/\*@\*\//,
1119
'import "https://cdn.jsdelivr.net/npm/sabayon/dist/polyfill.js";'
1220
);
1321
}
14-
super(blobParts, options);
22+
super(blobParts, ...args);
1523
}
1624
};
1725

1826
// early patch Worker to bootstrap sabayon for service-worker
1927
globalThis.Worker = class extends Worker {
2028
constructor(url, ...args) {
21-
if (args.length) {
29+
if (isOptions(args)) {
2230
const sw = args[0].serviceWorker || args[0].service_worker;
2331
if (sw) sabayon(sw);
2432
}

0 commit comments

Comments
 (0)
0