Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(browser): Flush offline queue on flush and browser online event #14764

Merged
merged 4 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
},
};
};
}
Loading