[ServiceWorker] "unload" event and Service Workers (#731)

A common use case of IndexedDB is to save state during the "beforeunload" event. Because IndexedDB is asynchronous, this operation is [unsupported behavior according to the specs](http://lists.w3.org/Archives/Public/public-webapps/2012JanMar/0809.html) although it's hit or miss on  [browser implementations](http://vaughnroyko.com/idbonbeforeunload/).

I think this kind of "save on exit" operation could be possible with Service Workers but I want to confirm that there is consensus that this should be supported behavior.

Take for example a web game:

```js
// page context

navigator.serviceWorker.register('serviceWorker.js');

var saveData = new Uint8Array(); // memory store with binary data

window.onunload = function() {
  // postMessage should be synchronous in this context?
  navigator.serviceWorker.controller.postMessage({
    type: 'save',
    save: saveData,
    slot: 1
  });
};
```

```js
// serviceWorker.js

self.onmessage = function(event) {
  if (event.data.type === 'save') {
    // async saving of event.data.save
  }
};
```

---
Reply to this email directly or view it on GitHub:
https://github.com/slightlyoff/ServiceWorker/issues/731

Received on Friday, 14 August 2015 17:07:20 UTC