javascript - How to persist data in a Service Worker -
as of now, chrome not support passing additional data push notifications received gcm. have execute fetch
service worker whenever receive push notification. far, good.
but: need include request parameter in http request executed in fetch
. how tell service worker parameter?
what i've tried far
using postmessage
tell service worker request parameter:
var serviceworkerdata = {}; self.addeventlistener('message', function (evt) { console.log('service worker received', evt.data); serviceworkerdata = evt.data.mydata; }); self.addeventlistener('push', function(event) { event.waituntil ( fetch("http://my.url", { method: 'post', body: 'mydata=' + serviceworkerdata }).then(function(response) { //... }) ); });
why not working
but not persistent, i.e. after close browser , open again, serviceworkerdata
lost. localstorage
not available within service workers, how persistence here?
you can use indexeddb.
https://github.com/mozilla/localforage if want simpler interface indexeddb.
you can import localforage lib in service worker using importscripts, see example: https://github.com/marco-c/mercurius/blob/master/static/sw-push.js.
Comments
Post a Comment