Skip to content
This repository has been archived by the owner on Apr 24, 2021. It is now read-only.

Be careful about JSON.stringify for PushSubscription objects #58

Open
owencm opened this issue Mar 25, 2016 · 1 comment
Open

Be careful about JSON.stringify for PushSubscription objects #58

owencm opened this issue Mar 25, 2016 · 1 comment
Labels

Comments

@owencm
Copy link
Contributor

owencm commented Mar 25, 2016

Learned something interesting today we need to handle:

  • In Chrome 43 through Chrome 45 JSON.stringify returns an empty object for PushSubscription objects. The recomended workaround is to reconstruct the object before stringifying, i.e. const fixedSub = { endpoint: sub.endpoint, registrationId: sub.registrationId }
  • In Chrome 51+ you cannot directly access the keys property on PushSubscription objects but need to JSON.stringify first

Hence I think the best thing to do is something like

let pushSub = await getPushSubscription();
pushSub = Object.assign({ endpoint: pushSub.endpoint, registrationId: pushSub.registrationId }, JSON.parse(JSON.stringify(pushSub)));

I haven't tried it but so far as I can see that should always ensure we get the keys we want working around the bugs and weirdnesses of other versions?

Worth noting that with this solution registrationId will always be a key but will be undefined from the version of Chrome which deprecated it (46 or so?). I suspect the better solution is to deprecate registrationId with respect to this library and thus just remove the extraction of that key from the above.

@gauntface
Copy link
Contributor

Note to assignee: Manage when subscription id is separate or not.

@addyosmani addyosmani added the bug label Apr 4, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants