Skip to content

Commit

Permalink
feat(browser): Flush offline queue on flush and browser online ev…
Browse files Browse the repository at this point in the history
…ent (#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
  • Loading branch information
timfish authored Dec 18, 2024
1 parent cb1dcf7 commit 2628e40
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
11 changes: 10 additions & 1 deletion packages/browser/src/transports/offline.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { BaseTransportOptions, Envelope, OfflineStore, OfflineTransportOptions, Transport } from '@sentry/core';
import { makeOfflineTransport, parseEnvelope, serializeEnvelope } from '@sentry/core';
import { WINDOW } from '../helpers';
import { makeFetchTransport } from './fetch';

// 'Store', 'promisifyRequest' and 'createStore' were originally copied from the 'idb-keyval' package before being
Expand Down Expand Up @@ -158,7 +159,15 @@ function createIndexedDbStore(options: BrowserOfflineTransportOptions): OfflineS
function makeIndexedDbOfflineTransport<T>(
createTransport: (options: T) => Transport,
): (options: T & BrowserOfflineTransportOptions) => Transport {
return options => createTransport({ ...options, createStore: createIndexedDbStore });
return options => {
const transport = createTransport({ ...options, createStore: createIndexedDbStore });

WINDOW.addEventListener('online', async _ => {
await transport.flush();
});

return transport;
};
}

/**
Expand Down
29 changes: 29 additions & 0 deletions packages/browser/test/transports/offline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ describe('makeOfflineTransport', () => {
await deleteDatabase('sentry');
(global as any).TextEncoder = TextEncoder;
(global as any).TextDecoder = TextDecoder;
(global as any).addEventListener = () => {};
});

it('indexedDb wrappers push, unshift and pop', async () => {
Expand Down Expand Up @@ -115,4 +116,32 @@ describe('makeOfflineTransport', () => {
expect(queuedCount).toEqual(1);
expect(getSendCount()).toEqual(2);
});

it('flush forces retry', async () => {
const { getSendCount, baseTransport } = createTestTransport(new Error(), { statusCode: 200 }, { statusCode: 200 });
let queuedCount = 0;
const transport = makeBrowserOfflineTransport(baseTransport)({
...transportOptions,
shouldStore: () => {
queuedCount += 1;
return true;
},
url: 'http://localhost',
});
const result = await transport.send(ERROR_ENVELOPE);

expect(result).toEqual({});

await delay(MIN_DELAY * 2);

expect(getSendCount()).toEqual(0);
expect(queuedCount).toEqual(1);

await transport.flush();

await delay(MIN_DELAY * 2);

expect(queuedCount).toEqual(1);
expect(getSendCount()).toEqual(1);
});
});
10 changes: 9 additions & 1 deletion packages/core/src/transports/offline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,15 @@ export function makeOfflineTransport<TO>(

return {
send,
flush: t => transport.flush(t),
flush: timeout => {
// If there's no timeout, we should attempt to flush the offline queue.
if (timeout === undefined) {
retryDelay = START_DELAY;
flushIn(MIN_DELAY);
}

return transport.flush(timeout);
},
};
};
}

0 comments on commit 2628e40

Please sign in to comment.