diff --git a/packages/browser/src/transports/offline.ts b/packages/browser/src/transports/offline.ts index 372c360194c7..5fbf7fa6ffc4 100644 --- a/packages/browser/src/transports/offline.ts +++ b/packages/browser/src/transports/offline.ts @@ -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 @@ -158,7 +159,15 @@ function createIndexedDbStore(options: BrowserOfflineTransportOptions): OfflineS function makeIndexedDbOfflineTransport( 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; + }; } /** diff --git a/packages/browser/test/transports/offline.test.ts b/packages/browser/test/transports/offline.test.ts index a9a396949588..070d6623f967 100644 --- a/packages/browser/test/transports/offline.test.ts +++ b/packages/browser/test/transports/offline.test.ts @@ -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 () => { @@ -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); + }); }); diff --git a/packages/core/src/transports/offline.ts b/packages/core/src/transports/offline.ts index cf8902739db7..4cdd0b4a71af 100644 --- a/packages/core/src/transports/offline.ts +++ b/packages/core/src/transports/offline.ts @@ -170,7 +170,15 @@ export function makeOfflineTransport( 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); + }, }; }; }