Skip to content

Commit 5238091

Browse files
committed
fix(replay): Ensure initial event is compressed
1 parent ec27c34 commit 5238091

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

packages/replay/src/eventBuffer/EventBufferProxy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class EventBufferProxy implements EventBuffer {
2020
this._compression = new EventBufferCompressionWorker(worker);
2121
this._used = this._fallback;
2222

23-
void this._ensureWorkerIsLoaded();
23+
void this.ensureWorkerIsLoaded();
2424
}
2525

2626
/** @inheritDoc */
@@ -60,7 +60,7 @@ export class EventBufferProxy implements EventBuffer {
6060
}
6161

6262
/** Ensure the worker has loaded. */
63-
private async _ensureWorkerIsLoaded(): Promise<void> {
63+
public async ensureWorkerIsLoaded(): Promise<void> {
6464
try {
6565
await this._compression.ensureReady();
6666
} catch (error) {

packages/replay/src/replay.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ import { disabledUntil, logger } from '@sentry/utils';
66
import { EventType, record } from 'rrweb';
77

88
import {
9+
ERROR_CHECKOUT_TIME,
910
MAX_SESSION_LIFE,
1011
SESSION_IDLE_DURATION,
1112
VISIBILITY_CHANGE_TIMEOUT,
1213
WINDOW,
13-
ERROR_CHECKOUT_TIME,
1414
} from './constants';
1515
import { setupPerformanceObserver } from './coreHandlers/performanceObserver';
1616
import { createEventBuffer } from './eventBuffer';
17+
import { EventBufferProxy } from './eventBuffer/EventBufferProxy';
1718
import { getSession } from './session/getSession';
1819
import { saveSession } from './session/saveSession';
1920
import type {
@@ -328,9 +329,18 @@ export class ReplayContainer implements ReplayContainerInterface {
328329
* from calling both `flush` and `_debouncedFlush`. Otherwise, there could be
329330
* cases of mulitple flushes happening closely together.
330331
*/
331-
public flushImmediate(): Promise<void> {
332+
public async flushImmediate(waitForCompression?: boolean): Promise<void> {
332333
this._debouncedFlush();
333334
// `.flush` is provided by the debounced function, analogously to lodash.debounce
335+
336+
// Ensure the worker is loaded, so the sent event is compressed
337+
if (waitForCompression && this.eventBuffer instanceof EventBufferProxy) {
338+
try {
339+
await this.eventBuffer.ensureWorkerIsLoaded();
340+
} catch (error) {
341+
// If this fails, we'll just send uncompressed events
342+
}
343+
}
334344
return this._debouncedFlush.flush() as Promise<void>;
335345
}
336346

@@ -541,7 +551,8 @@ export class ReplayContainer implements ReplayContainerInterface {
541551
// replays (e.g. opening and closing a tab quickly), but these can be
542552
// filtered on the UI.
543553
if (this.recordingMode === 'session') {
544-
void this.flushImmediate();
554+
// We want to ensure the worker is ready, as otherwise we'd always send the first event uncompressed
555+
void this.flushImmediate(true);
545556
}
546557

547558
return true;

packages/replay/test/integration/errorSampleRate.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import { captureException } from '@sentry/core';
22

33
import {
44
DEFAULT_FLUSH_MIN_DELAY,
5+
ERROR_CHECKOUT_TIME,
56
REPLAY_SESSION_KEY,
67
VISIBILITY_CHANGE_TIMEOUT,
78
WINDOW,
8-
ERROR_CHECKOUT_TIME,
99
} from '../../src/constants';
1010
import type { ReplayContainer } from '../../src/replay';
1111
import { addEvent } from '../../src/util/addEvent';

0 commit comments

Comments
 (0)