Re: [w3c/ServiceWorker] How can I pass or post params to sevirce worker on register or install? (#1441)

How about this:

HTML:

```html
<!DOCTYPE html>
<h1>See console</h1>
<script>
  navigator.serviceWorker.register('sw.js').then(reg => {
    reg.installing.postMessage({ type: 'install-data', value: 'hello!' });
  });
</script>
```

sw.js:

```js
addEventListener('install', event => {
  event.waitUntil(async function() {
    const value = await installData;
    console.log('Service worker saw data:', value);
  }());
});

const installData = new Promise(resolve => {
  addEventListener('message', (event) => {
    if ('type' in event.data && event.data.type === 'install-data') {
      resolve(event.data.value);
    }
  });
});
```

Live example: https://static-misc.glitch.me/sw-postmessage-install/

Please reopen if this doesn't help.

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/w3c/ServiceWorker/issues/1441#issuecomment-504932932

Received on Monday, 24 June 2019 09:28:30 UTC