Only resubscribe on compatible old subscriptions (#616)

This change makes resubscriptions backwards compatible by only running the new resubscribe code if the old push subscription was saved under service worker version 2.

Co-authored-by: ekzyis <ek@stacker.news>
This commit is contained in:
ekzyis 2023-11-09 01:18:41 +01:00 committed by GitHub
parent 41a1c686dc
commit 0b872cbbe7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 3 deletions

View File

@ -126,8 +126,11 @@ export function onPushSubscriptionChange (sw) {
oldSubscription ??= await storage.getItem('subscription')
newSubscription ??= await sw.registration.pushManager.getSubscription()
if (!newSubscription) {
if (isSync && oldSubscription) {
// service worker lost the push subscription somehow
if (isSync && oldSubscription?.swVersion === 2) {
// service worker lost the push subscription somehow, we assume this is a bug -> resubscribe
// see https://github.com/stackernews/stacker.news/issues/411#issuecomment-1790675861
// NOTE: this is only run on IndexedDB subscriptions stored under service worker version 2 since this is not backwards compatible
// see discussion in https://github.com/stackernews/stacker.news/pull/597
messageChannelPort?.postMessage({ message: '[sw:handlePushSubscriptionChange] service worker lost subscription' })
actionChannelPort?.postMessage({ action: 'RESUBSCRIBE' })
return
@ -180,7 +183,7 @@ export function onMessage (sw) {
messageChannelPort?.postMessage({ message: '[sw:message] received message', context: { action: event.data.action } })
if (event.data.action === 'STORE_SUBSCRIPTION') {
messageChannelPort?.postMessage({ message: '[sw:message] storing subscription in IndexedDB', context: { endpoint: event.data.subscription.endpoint } })
return event.waitUntil(storage.setItem('subscription', event.data.subscription))
return event.waitUntil(storage.setItem('subscription', { ...event.data.subscription, swVersion: 2 }))
}
if (event.data.action === 'SYNC_SUBSCRIPTION') {
return event.waitUntil(onPushSubscriptionChange(sw)(event, true))