Skip to content

Commit 2628e40

Browse files
authoredDec 18, 2024
feat(browser): Flush offline queue on flush and browser online event (#14764)
This PR: - Resets the offline queue time on `flush` so that sending is retried - In the browser calls `flush` when the `online` event is fired
1 parent cb1dcf7 commit 2628e40

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed
 

‎packages/browser/src/transports/offline.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { BaseTransportOptions, Envelope, OfflineStore, OfflineTransportOptions, Transport } from '@sentry/core';
22
import { makeOfflineTransport, parseEnvelope, serializeEnvelope } from '@sentry/core';
3+
import { WINDOW } from '../helpers';
34
import { makeFetchTransport } from './fetch';
45

56
// 'Store', 'promisifyRequest' and 'createStore' were originally copied from the 'idb-keyval' package before being
@@ -158,7 +159,15 @@ function createIndexedDbStore(options: BrowserOfflineTransportOptions): OfflineS
158159
function makeIndexedDbOfflineTransport<T>(
159160
createTransport: (options: T) => Transport,
160161
): (options: T & BrowserOfflineTransportOptions) => Transport {
161-
return options => createTransport({ ...options, createStore: createIndexedDbStore });
162+
return options => {
163+
const transport = createTransport({ ...options, createStore: createIndexedDbStore });
164+
165+
WINDOW.addEventListener('online', async _ => {
166+
await transport.flush();
167+
});
168+
169+
return transport;
170+
};
162171
}
163172

164173
/**

‎packages/browser/test/transports/offline.test.ts

+29
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ describe('makeOfflineTransport', () => {
6464
await deleteDatabase('sentry');
6565
(global as any).TextEncoder = TextEncoder;
6666
(global as any).TextDecoder = TextDecoder;
67+
(global as any).addEventListener = () => {};
6768
});
6869

6970
it('indexedDb wrappers push, unshift and pop', async () => {
@@ -115,4 +116,32 @@ describe('makeOfflineTransport', () => {
115116
expect(queuedCount).toEqual(1);
116117
expect(getSendCount()).toEqual(2);
117118
});
119+
120+
it('flush forces retry', async () => {
121+
const { getSendCount, baseTransport } = createTestTransport(new Error(), { statusCode: 200 }, { statusCode: 200 });
122+
let queuedCount = 0;
123+
const transport = makeBrowserOfflineTransport(baseTransport)({
124+
...transportOptions,
125+
shouldStore: () => {
126+
queuedCount += 1;
127+
return true;
128+
},
129+
url: 'http://localhost',
130+
});
131+
const result = await transport.send(ERROR_ENVELOPE);
132+
133+
expect(result).toEqual({});
134+
135+
await delay(MIN_DELAY * 2);
136+
137+
expect(getSendCount()).toEqual(0);
138+
expect(queuedCount).toEqual(1);
139+
140+
await transport.flush();
141+
142+
await delay(MIN_DELAY * 2);
143+
144+
expect(queuedCount).toEqual(1);
145+
expect(getSendCount()).toEqual(1);
146+
});
118147
});

‎packages/core/src/transports/offline.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,15 @@ export function makeOfflineTransport<TO>(
170170

171171
return {
172172
send,
173-
flush: t => transport.flush(t),
173+
flush: timeout => {
174+
// If there's no timeout, we should attempt to flush the offline queue.
175+
if (timeout === undefined) {
176+
retryDelay = START_DELAY;
177+
flushIn(MIN_DELAY);
178+
}
179+
180+
return transport.flush(timeout);
181+
},
174182
};
175183
};
176184
}

0 commit comments

Comments
 (0)