[Webkit-unassigned] [Bug 213984] New: ServiceWorker.postMessage() doesn't work from inside iframe

bugzilla-daemon at webkit.org bugzilla-daemon at webkit.org
Sun Jul 5 22:08:04 PDT 2020


https://bugs.webkit.org/show_bug.cgi?id=213984

            Bug ID: 213984
           Summary: ServiceWorker.postMessage() doesn't work from inside
                    iframe
           Product: WebKit
           Version: Safari Technology Preview
          Hardware: Unspecified
                OS: Unspecified
            Status: NEW
          Severity: Normal
          Priority: P2
         Component: Service Workers
          Assignee: webkit-unassigned at lists.webkit.org
          Reporter: nkronlage at hotmail.com

Created attachment 403573

  --> https://bugs.webkit.org/attachment.cgi?id=403573&action=review

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>

-- 
You are receiving this mail because:
You are the assignee for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-unassigned/attachments/20200706/7ef72ff7/attachment.htm>


More information about the webkit-unassigned mailing list