RESOLVED FIXED 213984
ServiceWorker.postMessage() doesn't work from inside iframe
https://bugs.webkit.org/show_bug.cgi?id=213984
Summary ServiceWorker.postMessage() doesn't work from inside iframe
nkronlage
Reported 2020-07-05 22:08:04 PDT
Created attachment 403573 [details] Simplified repro Sending messages via postMessage to a ServiceWorker does not work from inside an iframe. To repro this, download the attached zip. Serve the files locally. Open from-iframe.html in Safari Go to Develop->Service Workers->localhost Go to Console and notice there are no 'postMessage received:' logs here. If you open from-main-window.html, you will see the postMessage events. This works correctly in FireFox. Chrome has a bug registering ServiceWorkers from iframes so it doesn't make it to the point of postMessage - https://bugs.chromium.org/p/chromium/issues/detail?id=1102209 from-iframe.html: <iframe></iframe> <script> const iframe = document.querySelector('iframe'); iframe.contentDocument.write(` <body> <script> (async () => { document.body.insertAdjacentHTML('beforeend', 'registering service-worker.js<br>'); const reg = await navigator.serviceWorker.register('/service-worker.js'); const sw = reg.installing; sw.postMessage('after register'); sw.onstatechange = () => { document.body.insertAdjacentHTML('beforeend', \`statechange to: \${sw.state}<br>\`); sw.postMessage('statechange to ' + sw.state); }; })(); </${'script'}> </body>`); </script> service-worker.js: console.log('service worker loaded'); addEventListener('message', event => { console.log('postMessage received:', event.data); }); from-main-window.html: <body> <script> (async () => { document.body.insertAdjacentHTML('beforeend', 'registering service-worker.js<br>'); const reg = await navigator.serviceWorker.register('/service-worker.js'); const sw = reg.installing; sw.postMessage('after register'); sw.onstatechange = () => { document.body.insertAdjacentHTML('beforeend', `statechange to: ${sw.state}<br>`); sw.postMessage('statechange to ' + sw.state); }; })(); </script> </body>
Attachments
Simplified repro (1.24 KB, application/zip)
2020-07-05 22:08 PDT, nkronlage
no flags
Patch (17.50 KB, patch)
2020-08-05 12:56 PDT, youenn fablet
no flags
Patch (40.01 KB, patch)
2022-04-14 00:33 PDT, youenn fablet
no flags
Patch (40.26 KB, patch)
2022-04-14 01:46 PDT, youenn fablet
no flags
Patch (40.50 KB, patch)
2022-04-14 02:33 PDT, youenn fablet
no flags
Patch (41.85 KB, patch)
2022-04-14 06:21 PDT, youenn fablet
no flags
Patch for landing (41.83 KB, patch)
2022-04-15 00:44 PDT, youenn fablet
no flags
Patch (41.98 KB, patch)
2022-04-15 01:56 PDT, youenn fablet
no flags
Radar WebKit Bug Importer
Comment 1 2020-07-07 18:44:40 PDT
youenn fablet
Comment 2 2020-08-05 07:43:14 PDT
Issue is that the iframe is not registered as a service worker client.
youenn fablet
Comment 3 2020-08-05 12:56:39 PDT
youenn fablet
Comment 4 2022-04-14 00:33:46 PDT
youenn fablet
Comment 5 2022-04-14 01:46:44 PDT
youenn fablet
Comment 6 2022-04-14 02:33:32 PDT
youenn fablet
Comment 7 2022-04-14 06:21:32 PDT
Chris Dumez
Comment 8 2022-04-14 08:27:33 PDT
Comment on attachment 457618 [details] Patch r=me
youenn fablet
Comment 9 2022-04-15 00:43:41 PDT
The check to reduce service worker client registrations out of http/blob/about is not working with extension schemes. I'll remove it.
youenn fablet
Comment 10 2022-04-15 00:44:14 PDT
Created attachment 457681 [details] Patch for landing
youenn fablet
Comment 11 2022-04-15 01:56:20 PDT
EWS
Comment 12 2022-04-15 06:16:39 PDT
Committed r292905 (249675@main): <https://commits.webkit.org/249675@main> All reviewed patches have been landed. Closing bug and clearing flags on attachment 457685 [details].
Note You need to log in before you can comment on or make changes to this bug.