Skip to content

Commit 57125f4

Browse files
committed
Merge pull request #98069 from thedinosoar/fix/service-worker-sandbox-error-98068
[Web] Fix PWA callback assignment causing crash in sandboxed iframes
2 parents 81733e6 + 05b266b commit 57125f4

File tree

4 files changed

+48
-25
lines changed

4 files changed

+48
-25
lines changed

misc/dist/html/editor.html

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -363,24 +363,28 @@ <h2 id="welcome-modal-title" class="welcome-modal-title">Important - Please read
363363
btn.style.display = '';
364364
}
365365
if ('serviceWorker' in navigator) {
366-
navigator.serviceWorker.register('service.worker.js').then(function (reg) {
367-
if (reg.waiting) {
368-
notifyUpdate(reg.waiting);
369-
}
370-
reg.addEventListener('updatefound', function () {
371-
const update = reg.installing;
372-
update.addEventListener('statechange', function () {
373-
if (update.state === 'installed') {
374-
// It's a new install, claim and perform aggressive caching.
375-
if (!reg.active) {
376-
update.postMessage('claim');
377-
} else {
378-
notifyUpdate(update);
366+
try {
367+
navigator.serviceWorker.register('service.worker.js').then(function (reg) {
368+
if (reg.waiting) {
369+
notifyUpdate(reg.waiting);
370+
}
371+
reg.addEventListener('updatefound', function () {
372+
const update = reg.installing;
373+
update.addEventListener('statechange', function () {
374+
if (update.state === 'installed') {
375+
// It's a new install, claim and perform aggressive caching.
376+
if (!reg.active) {
377+
update.postMessage('claim');
378+
} else {
379+
notifyUpdate(update);
380+
}
379381
}
380-
}
382+
});
381383
});
382384
});
383-
});
385+
} catch (e) {
386+
console.error('Error while registering service worker:', e);
387+
}
384388
}
385389

386390
const missing = Engine.getMissingFeatures({

misc/dist/html/full-size.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,15 @@
152152

153153
if (missing.length !== 0) {
154154
if (GODOT_CONFIG['serviceWorker'] && GODOT_CONFIG['ensureCrossOriginIsolationHeaders'] && 'serviceWorker' in navigator) {
155+
let serviceWorkerRegistrationPromise;
156+
try {
157+
serviceWorkerRegistrationPromise = navigator.serviceWorker.getRegistration();
158+
} catch (err) {
159+
serviceWorkerRegistrationPromise = Promise.reject(new Error('Service worker registration failed.'));
160+
}
155161
// There's a chance that installing the service worker would fix the issue
156162
Promise.race([
157-
navigator.serviceWorker.getRegistration().then((registration) => {
163+
serviceWorkerRegistrationPromise.then((registration) => {
158164
if (registration != null) {
159165
return Promise.reject(new Error('Service worker already exists.'));
160166
}

platform/web/js/engine/engine.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,11 @@ const Engine = (function () {
241241
*/
242242
installServiceWorker: function () {
243243
if (this.config.serviceWorker && 'serviceWorker' in navigator) {
244-
return navigator.serviceWorker.register(this.config.serviceWorker);
244+
try {
245+
return navigator.serviceWorker.register(this.config.serviceWorker);
246+
} catch (e) {
247+
return Promise.reject(e);
248+
}
245249
}
246250
return Promise.resolve();
247251
},

platform/web/js/libs/library_godot_os.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -441,21 +441,30 @@ const GodotPWA = {
441441
godot_js_pwa_cb__sig: 'vi',
442442
godot_js_pwa_cb: function (p_update_cb) {
443443
if ('serviceWorker' in navigator) {
444-
const cb = GodotRuntime.get_func(p_update_cb);
445-
navigator.serviceWorker.getRegistration().then(GodotPWA.updateState.bind(null, cb));
444+
try {
445+
const cb = GodotRuntime.get_func(p_update_cb);
446+
navigator.serviceWorker.getRegistration().then(GodotPWA.updateState.bind(null, cb));
447+
} catch (e) {
448+
GodotRuntime.error('Failed to assign PWA callback', e);
449+
}
446450
}
447451
},
448452

449453
godot_js_pwa_update__proxy: 'sync',
450454
godot_js_pwa_update__sig: 'i',
451455
godot_js_pwa_update: function () {
452456
if ('serviceWorker' in navigator && GodotPWA.hasUpdate) {
453-
navigator.serviceWorker.getRegistration().then(function (reg) {
454-
if (!reg || !reg.waiting) {
455-
return;
456-
}
457-
reg.waiting.postMessage('update');
458-
});
457+
try {
458+
navigator.serviceWorker.getRegistration().then(function (reg) {
459+
if (!reg || !reg.waiting) {
460+
return;
461+
}
462+
reg.waiting.postMessage('update');
463+
});
464+
} catch (e) {
465+
GodotRuntime.error(e);
466+
return 1;
467+
}
459468
return 0;
460469
}
461470
return 1;

0 commit comments

Comments
 (0)