|
1 | 1 | import ServiceWorkerStorage from 'serviceworker-storage'
|
2 | 2 | import { numWithUnits } from '@/lib/format'
|
3 | 3 | import { CLEAR_NOTIFICATIONS, clearAppBadge, setAppBadge } from '@/lib/badge'
|
4 |
| -import { ACTION_PORT, DELETE_SUBSCRIPTION, STORE_SUBSCRIPTION, SYNC_SUBSCRIPTION } from '@/components/serviceworker' |
| 4 | +import { DELETE_SUBSCRIPTION, STORE_SUBSCRIPTION } from '@/components/serviceworker' |
5 | 5 |
|
6 |
| -// we store existing push subscriptions and OS to keep them in sync with server |
| 6 | +// we store existing push subscriptions for the onpushsubscriptionchange event |
7 | 7 | const storage = new ServiceWorkerStorage('sw:storage', 1)
|
8 | 8 |
|
9 |
| -// for communication between app and service worker |
10 |
| -// see https://developer.mozilla.org/en-US/docs/Web/API/MessageChannel |
11 |
| -let actionChannelPort |
12 |
| - |
13 | 9 | // current push notification count for badge purposes
|
14 | 10 | let activeCount = 0
|
15 | 11 |
|
@@ -130,28 +126,14 @@ export function onNotificationClick (sw) {
|
130 | 126 |
|
131 | 127 | export function onPushSubscriptionChange (sw) {
|
132 | 128 | // https://medium.com/@madridserginho/how-to-handle-webpush-api-pushsubscriptionchange-event-in-modern-browsers-6e47840d756f
|
133 |
| - // `isSync` is passed if function was called because of 'SYNC_SUBSCRIPTION' event |
134 |
| - // this makes sure we can differentiate between 'pushsubscriptionchange' events and our custom 'SYNC_SUBSCRIPTION' event |
135 |
| - return async (event, isSync) => { |
| 129 | + return async (event) => { |
136 | 130 | let { oldSubscription, newSubscription } = event
|
137 | 131 | // https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerGlobalScope/pushsubscriptionchange_event
|
138 | 132 | // fallbacks since browser may not set oldSubscription and newSubscription
|
139 | 133 | oldSubscription ??= await storage.getItem('subscription')
|
140 | 134 | newSubscription ??= await sw.registration.pushManager.getSubscription()
|
141 |
| - if (!newSubscription) { |
142 |
| - if (isSync && oldSubscription?.swVersion === 2) { |
143 |
| - // service worker lost the push subscription somehow, we assume this is a bug -> resubscribe |
144 |
| - // see https://github.com/stackernews/stacker.news/issues/411#issuecomment-1790675861 |
145 |
| - // NOTE: this is only run on IndexedDB subscriptions stored under service worker version 2 since this is not backwards compatible |
146 |
| - // see discussion in https://github.com/stackernews/stacker.news/pull/597 |
147 |
| - actionChannelPort?.postMessage({ action: 'RESUBSCRIBE' }) |
148 |
| - return |
149 |
| - } |
150 |
| - // no subscription exists at the moment |
151 |
| - return |
152 |
| - } |
153 |
| - if (oldSubscription?.endpoint === newSubscription.endpoint) { |
154 |
| - // subscription did not change. no need to sync with server |
| 135 | + if (!newSubscription || oldSubscription?.endpoint === newSubscription.endpoint) { |
| 136 | + // no subscription exists at the moment or subscription did not change |
155 | 137 | return
|
156 | 138 | }
|
157 | 139 | // convert keys from ArrayBuffer to string
|
@@ -182,16 +164,9 @@ export function onPushSubscriptionChange (sw) {
|
182 | 164 |
|
183 | 165 | export function onMessage (sw) {
|
184 | 166 | return async (event) => {
|
185 |
| - if (event.data.action === ACTION_PORT) { |
186 |
| - actionChannelPort = event.ports[0] |
187 |
| - return |
188 |
| - } |
189 | 167 | if (event.data.action === STORE_SUBSCRIPTION) {
|
190 | 168 | return event.waitUntil(storage.setItem('subscription', { ...event.data.subscription, swVersion: 2 }))
|
191 | 169 | }
|
192 |
| - if (event.data.action === SYNC_SUBSCRIPTION) { |
193 |
| - return event.waitUntil(onPushSubscriptionChange(sw)(event, true)) |
194 |
| - } |
195 | 170 | if (event.data.action === DELETE_SUBSCRIPTION) {
|
196 | 171 | return event.waitUntil(storage.removeItem('subscription'))
|
197 | 172 | }
|
|
0 commit comments