Skip to content

Commit cd73722

Browse files
committed
fix: call sendBeacon synchronously
1 parent f1ebe53 commit cd73722

File tree

2 files changed

+34
-21
lines changed

2 files changed

+34
-21
lines changed

src/dispatch/Dispatch.ts

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,26 @@ export class Dispatch {
159159
return this.dispatchBeacon().catch(() => {});
160160
};
161161

162+
private flushSync: EventListener = () => {
163+
if (document.visibilityState === 'hidden') {
164+
if (this.doRequest()) {
165+
let flush = this.rum.sendBeacon;
166+
let backup = this.rum.sendFetch;
167+
168+
if (!this.config.useBeacon) {
169+
[flush, backup] = [backup, flush];
170+
}
171+
172+
const req = this.createRequest();
173+
flush(req)
174+
.catch(() => backup(req))
175+
.catch(() => {
176+
// fail silent
177+
});
178+
}
179+
}
180+
};
181+
162182
/**
163183
* Automatically dispatch cached events.
164184
*/
@@ -177,19 +197,12 @@ export class Dispatch {
177197
//
178198
// A third option is to send both, however this would increase
179199
// bandwitch and require deduping server side.
180-
this.config.useBeacon
181-
? this.dispatchBeaconFailSilent
182-
: this.dispatchFetchFailSilent
200+
this.flushSync
183201
);
184202
// Using 'pagehide' is redundant most of the time (visibilitychange is
185203
// always fired before pagehide) but older browsers may support
186204
// 'pagehide' but not 'visibilitychange'.
187-
document.addEventListener(
188-
'pagehide',
189-
this.config.useBeacon
190-
? this.dispatchBeaconFailSilent
191-
: this.dispatchFetchFailSilent
192-
);
205+
document.addEventListener('pagehide', this.flushSync);
193206
if (this.config.dispatchInterval <= 0 || this.dispatchTimerId) {
194207
return;
195208
}
@@ -203,18 +216,8 @@ export class Dispatch {
203216
* Stop automatically dispatching cached events.
204217
*/
205218
public stopDispatchTimer() {
206-
document.removeEventListener(
207-
'visibilitychange',
208-
this.config.useBeacon
209-
? this.dispatchBeaconFailSilent
210-
: this.dispatchFetchFailSilent
211-
);
212-
document.removeEventListener(
213-
'pagehide',
214-
this.config.useBeacon
215-
? this.dispatchBeaconFailSilent
216-
: this.dispatchFetchFailSilent
217-
);
219+
document.removeEventListener('visibilitychange', this.flushSync);
220+
document.removeEventListener('pagehide', this.flushSync);
218221
if (this.dispatchTimerId) {
219222
window.clearInterval(this.dispatchTimerId);
220223
this.dispatchTimerId = undefined;

src/dispatch/__tests__/Dispatch.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,17 @@ jest.mock('../DataPlaneClient', () => ({
1414
.mockImplementation(() => ({ sendFetch, sendBeacon }))
1515
}));
1616

17+
let visibilityState = 'visible';
18+
Object.defineProperty(document, 'visibilityState', {
19+
configurable: true,
20+
get: () => visibilityState
21+
});
22+
1723
describe('Dispatch tests', () => {
1824
beforeEach(() => {
1925
sendFetch.mockClear();
2026
sendBeacon.mockClear();
27+
visibilityState = 'visible';
2128

2229
(DataPlaneClient as any).mockImplementation(() => {
2330
return {
@@ -262,6 +269,7 @@ describe('Dispatch tests', () => {
262269

263270
test('when visibilitychange event is triggered then beacon dispatch runs', async () => {
264271
// Init
272+
visibilityState = 'hidden';
265273
const dispatch = new Dispatch(
266274
Utils.AWS_RUM_REGION,
267275
Utils.AWS_RUM_ENDPOINT,
@@ -283,6 +291,7 @@ describe('Dispatch tests', () => {
283291

284292
test('when useBeacon is false then visibilitychange uses fetch dispatch', async () => {
285293
// Init
294+
visibilityState = 'hidden';
286295
const dispatch = new Dispatch(
287296
Utils.AWS_RUM_REGION,
288297
Utils.AWS_RUM_ENDPOINT,
@@ -305,6 +314,7 @@ describe('Dispatch tests', () => {
305314

306315
test('when useBeacon is false then pagehide uses fetch dispatch', async () => {
307316
// Init
317+
visibilityState = 'hidden';
308318
const dispatch = new Dispatch(
309319
Utils.AWS_RUM_REGION,
310320
Utils.AWS_RUM_ENDPOINT,

0 commit comments

Comments
 (0)