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

Be careful about JSON.stringify for PushSubscription objects #58

@owencm

Description

@owencm

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions