Skip to content

Commit ec902eb

Browse files
ekzyishuumn
andauthored
Remove unnecessary service worker message types (#2268)
* Remove service worker logger * Use async/await for togglePushSubscription * Remove commented out logger calls in service worker * Remove message channel between service worker and app The listener for messages from the service worker was removed in a previous commit. * Remove unused OS detection for service worker * Formatting * Remove unnecessary service worker message types These messages types were meant to debug or fix push notifications getting lost. We figured out the issue: push notifications were lost because of silent pushes. So these message types are no longer needed. --------- Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com>
1 parent 8382dda commit ec902eb

File tree

2 files changed

+12
-59
lines changed

2 files changed

+12
-59
lines changed

components/serviceworker.js

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@ const applicationServerKey = process.env.NEXT_PUBLIC_VAPID_PUBKEY
77
const ServiceWorkerContext = createContext()
88

99
// message types for communication between app and service worker
10-
export const ACTION_PORT = 'ACTION_PORT' // message to exchange action channel on which service worker will send actions back to app
11-
export const SYNC_SUBSCRIPTION = 'SYNC_SUBSCRIPTION' // trigger onPushSubscriptionChange event in service worker manually
12-
export const RESUBSCRIBE = 'RESUBSCRIBE' // trigger resubscribing to push notifications (sw -> app)
13-
export const DELETE_SUBSCRIPTION = 'DELETE_SUBSCRIPTION' // delete subscription in IndexedDB (app -> sw)
14-
export const STORE_SUBSCRIPTION = 'STORE_SUBSCRIPTION' // store subscription in IndexedDB (app -> sw)
10+
export const DELETE_SUBSCRIPTION = 'DELETE_SUBSCRIPTION'
11+
export const STORE_SUBSCRIPTION = 'STORE_SUBSCRIPTION'
1512

1613
export const ServiceWorkerProvider = ({ children }) => {
1714
const [registration, setRegistration] = useState(null)
@@ -35,12 +32,12 @@ export const ServiceWorkerProvider = ({ children }) => {
3532
`)
3633
const [deletePushSubscription] = useMutation(
3734
gql`
38-
mutation deletePushSubscription($endpoint: String!) {
39-
deletePushSubscription(endpoint: $endpoint) {
40-
id
41-
}
35+
mutation deletePushSubscription($endpoint: String!) {
36+
deletePushSubscription(endpoint: $endpoint) {
37+
id
4238
}
43-
`)
39+
}
40+
`)
4441

4542
// I am not entirely sure if this is needed since at least in Brave,
4643
// using `registration.pushManager.subscribe` also prompts the user.
@@ -94,8 +91,6 @@ export const ServiceWorkerProvider = ({ children }) => {
9491
await subscription.unsubscribe()
9592
const { endpoint } = subscription
9693
await deletePushSubscription({ variables: { endpoint } })
97-
// also delete push subscription in IndexedDB so we can tell if the user disabled push subscriptions
98-
// or we lost the push subscription due to a bug
9994
navigator.serviceWorker.controller.postMessage({ action: DELETE_SUBSCRIPTION })
10095
}
10196

@@ -130,23 +125,6 @@ export const ServiceWorkerProvider = ({ children }) => {
130125
})
131126
}, [])
132127

133-
useEffect(() => {
134-
// wait until successful registration
135-
if (!registration) return
136-
// setup channel between app and service worker
137-
const channel = new MessageChannel()
138-
navigator?.serviceWorker?.controller?.postMessage({ action: ACTION_PORT }, [channel.port2])
139-
channel.port1.onmessage = (event) => {
140-
if (event.data.action === RESUBSCRIBE && permission.notification === 'granted') {
141-
return subscribeToPushNotifications()
142-
}
143-
}
144-
// since (a lot of) browsers don't support the pushsubscriptionchange event,
145-
// we sync with server manually by checking on every page reload if the push subscription changed.
146-
// see https://medium.com/@madridserginho/how-to-handle-webpush-api-pushsubscriptionchange-event-in-modern-browsers-6e47840d756f
147-
navigator?.serviceWorker?.controller?.postMessage?.({ action: SYNC_SUBSCRIPTION })
148-
}, [registration, permission.notification])
149-
150128
const contextValue = useMemo(() => ({
151129
registration,
152130
support,

sw/eventListener.js

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
import ServiceWorkerStorage from 'serviceworker-storage'
22
import { numWithUnits } from '@/lib/format'
33
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'
55

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
77
const storage = new ServiceWorkerStorage('sw:storage', 1)
88

9-
// for communication between app and service worker
10-
// see https://developer.mozilla.org/en-US/docs/Web/API/MessageChannel
11-
let actionChannelPort
12-
139
// current push notification count for badge purposes
1410
let activeCount = 0
1511

@@ -130,28 +126,14 @@ export function onNotificationClick (sw) {
130126

131127
export function onPushSubscriptionChange (sw) {
132128
// 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) => {
136130
let { oldSubscription, newSubscription } = event
137131
// https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerGlobalScope/pushsubscriptionchange_event
138132
// fallbacks since browser may not set oldSubscription and newSubscription
139133
oldSubscription ??= await storage.getItem('subscription')
140134
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
155137
return
156138
}
157139
// convert keys from ArrayBuffer to string
@@ -182,16 +164,9 @@ export function onPushSubscriptionChange (sw) {
182164

183165
export function onMessage (sw) {
184166
return async (event) => {
185-
if (event.data.action === ACTION_PORT) {
186-
actionChannelPort = event.ports[0]
187-
return
188-
}
189167
if (event.data.action === STORE_SUBSCRIPTION) {
190168
return event.waitUntil(storage.setItem('subscription', { ...event.data.subscription, swVersion: 2 }))
191169
}
192-
if (event.data.action === SYNC_SUBSCRIPTION) {
193-
return event.waitUntil(onPushSubscriptionChange(sw)(event, true))
194-
}
195170
if (event.data.action === DELETE_SUBSCRIPTION) {
196171
return event.waitUntil(storage.removeItem('subscription'))
197172
}

0 commit comments

Comments
 (0)