Skip to content

Commit 633315d

Browse files
authored
fix: fall back to keepalive fetch when navigator.sendBeacon is unavailable
1 parent e195e30 commit 633315d

2 files changed

Lines changed: 43 additions & 17 deletions

File tree

‎src/utils/analytics/live-browser.ts‎

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -144,20 +144,28 @@ export function startLivePresence() {
144144
| undefined
145145
const sendLeave = () => {
146146
if (!lastPayload || !wasOnPublicPage) return
147-
navigator.sendBeacon(
148-
'/_a/live',
149-
new Blob(
150-
[
151-
JSON.stringify({
152-
...lastPayload,
153-
sequence: ++sequence,
154-
kind: 'leave',
155-
}),
156-
],
157-
{ type: 'application/json' },
158-
),
159-
)
147+
const body = JSON.stringify({
148+
...lastPayload,
149+
sequence: ++sequence,
150+
kind: 'leave',
151+
})
160152
wasOnPublicPage = false
153+
// Some clients (e.g. Apple Mail webviews, Firefox with beacons disabled)
154+
// have no Beacon API, so fall back to a keepalive fetch.
155+
if (typeof navigator.sendBeacon === 'function') {
156+
navigator.sendBeacon(
157+
'/_a/live',
158+
new Blob([body], { type: 'application/json' }),
159+
)
160+
return
161+
}
162+
void fetch('/_a/live', {
163+
method: 'POST',
164+
headers: { 'Content-Type': 'application/json' },
165+
body,
166+
keepalive: true,
167+
cache: 'no-store',
168+
}).catch(() => {})
161169
}
162170
report = (action) => send('update', action)
163171
const onVisibility = () => send('update')

‎tests/live-presence.test.ts‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ async function browser(
3939
cookie?: string
4040
hash?: string
4141
storage?: Map<string, string>
42+
noBeacon?: boolean
4243
} = {},
4344
) {
4445
const events: Blob[] = []
@@ -96,10 +97,14 @@ async function browser(
9697
navigator: {
9798
doNotTrack: options.dnt,
9899
globalPrivacyControl: options.gpc,
99-
sendBeacon(_url: string, body: Blob) {
100-
events.push(body)
101-
return true
102-
},
100+
...(options.noBeacon
101+
? {}
102+
: {
103+
sendBeacon(_url: string, body: Blob) {
104+
events.push(body)
105+
return true
106+
},
107+
}),
103108
},
104109
window: {
105110
addEventListener(name: string, handler: () => void) {
@@ -221,6 +226,19 @@ test('private navigation leaves the last public page and stops updates', async (
221226
visitor.stop()
222227
})
223228

229+
test('leaving falls back to fetch when sendBeacon is unavailable', async () => {
230+
const visitor = await browser({ noBeacon: true })
231+
assert.doesNotThrow(() => visitor.handlers.get('pagehide')?.())
232+
visitor.handlers.get('pagehide')?.()
233+
const messages = await visitor.messages()
234+
assert.deepEqual(
235+
messages.map((event) => event.kind),
236+
['update', 'leave'],
237+
)
238+
assert.equal(messages[1].sequence, 2)
239+
visitor.stop()
240+
})
241+
224242
test('tabs share a session but have independent presence', async () => {
225243
const first = await browser()
226244
const second = await browser({ cookie: first.document.cookie })

0 commit comments

Comments
 (0)