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 2 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
setTimeout(() => {
Sentry.captureMessage(`foo ${Math.random()}`);
}, 500);

setTimeout(() => {
Sentry.flush();
}, 2000);
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { expect } from '@playwright/test';
import type { Event } from '@sentry/core';

import { sentryTest } from '../../../../utils/fixtures';
import { getMultipleSentryEnvelopeRequests } from '../../../../utils/helpers';

function delay(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}

sentryTest('should flush event', async ({ getLocalTestUrl, page }) => {
// makeBrowserOfflineTransport is not included in any CDN bundles
if (process.env.PW_BUNDLE && process.env.PW_BUNDLE.startsWith('bundle')) {
sentryTest.skip();
}

const url = await getLocalTestUrl({ testDir: __dirname });

// This would be the obvious way to test offline support but it doesn't appear to work!
// await context.setOffline(true);

let abortedCount = 0;

// Abort all envelope requests so the event gets queued
await page.route(/ingest\.sentry\.io/, route => {
Fixed Show fixed Hide fixed
abortedCount += 1;
return route.abort();
});
await page.goto(url);
await delay(1_000);
await page.unroute(/ingest\.sentry\.io/);
Fixed Show fixed Hide fixed

expect(abortedCount).toBe(1);

// The previous event should now be queued

// It should get flushed after a few seconds
const eventData = await getMultipleSentryEnvelopeRequests<Event>(page, 2, { timeout: 4_000 });

// Filter out any client reports
const events = eventData.filter(e => !('discarded_events' in e)) as Event[];

expect(events).toHaveLength(1);

// The next two events will be message events starting with 'foo'
expect(events[0].message?.startsWith('foo'));
});
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 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 _ => {

Check failure on line 165 in packages/browser/src/transports/offline.ts

View workflow job for this annotation

GitHub Actions / Browser Unit Tests

test/transports/offline.test.ts > makeOfflineTransport > Queues and retries envelope if wrapped transport throws error

TypeError: WINDOW.addEventListener is not a function ❯ src/transports/offline.ts:165:12 ❯ test/transports/offline.test.ts:92:64
await transport.flush();
});

return transport;
};
}

/**
Expand Down
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