Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

perf(sdk): session replay takes a full snapshot every 5 minutes, not every flush (#614)

ayushjhanwar-png269f0c60dfb5
diff --git a/packages/sdks/web/src/index.ts b/packages/sdks/web/src/index.ts
index 4d4ecbdb4..e2846111c 100644
--- a/packages/sdks/web/src/index.ts
+++ b/packages/sdks/web/src/index.ts
@@ -26,6 +26,12 @@ export type SessionReplayOptions = {
blockClass?: string;
ignoreSelector?: string;
flushIntervalMs?: number;
+ /**
+ * How often (ms) a full snapshot of the page is recorded. Seeking replays
+ * from the closest earlier snapshot, so a shorter interval seeks faster in
+ * long sessions but stores far more. Defaults to 300000 (5 minutes).
+ */
+ fullSnapshotIntervalMs?: number;
maxEventsPerChunk?: number;
maxPayloadBytes?: number;
/**
diff --git a/packages/sdks/web/src/replay/recorder-options.test.ts b/packages/sdks/web/src/replay/recorder-options.test.ts
new file mode 100644
index 000000000..d01aaff05
--- /dev/null
+++ b/packages/sdks/web/src/replay/recorder-options.test.ts
@@ -0,0 +1,49 @@
+import { afterEach, beforeEach, describe, expect, mock, test } from 'bun:test';
+import { JSDOM } from 'jsdom';
+
+interface RecordOptions {
+ checkoutEveryNms?: number;
+}
+
+let recordOptions: RecordOptions = {};
+
+mock.module('rrweb', () => ({
+ record: (options: RecordOptions) => {
+ recordOptions = options;
+ return () => undefined;
+ },
+}));
+
+const { startReplayRecorder, stopReplayRecorder } = await import('./recorder');
+
+const FIVE_MINUTES_MS = 5 * 60 * 1000;
+
+let dom: JSDOM;
+
+beforeEach(() => {
+ dom = new JSDOM('<!doctype html><html><body></body></html>');
+ Object.assign(globalThis, {
+ window: dom.window,
+ document: dom.window.document,
+ });
+ recordOptions = {};
+});
+
+afterEach(() => {
+ stopReplayRecorder();
+ dom.window.close();
+});
+
+describe('full snapshot interval', () => {
+ test('defaults to five minutes, whatever the flush interval', () => {
+ startReplayRecorder({ flushIntervalMs: 5000 }, () => undefined);
+
+ expect(recordOptions.checkoutEveryNms).toBe(FIVE_MINUTES_MS);
+ });
+
+ test('can be set on its own', () => {
+ startReplayRecorder({ fullSnapshotIntervalMs: 60_000 }, () => undefined);
+
+ expect(recordOptions.checkoutEveryNms).toBe(60_000);
+ });
+});
diff --git a/packages/sdks/web/src/replay/recorder.ts b/packages/sdks/web/src/replay/recorder.ts
index 9917774d7..62a15538b 100644
--- a/packages/sdks/web/src/replay/recorder.ts
+++ b/packages/sdks/web/src/replay/recorder.ts
@@ -9,10 +9,17 @@ export type ReplayRecorderConfig = {
blockClass?: string;
ignoreSelector?: string;
flushIntervalMs?: number;
+ fullSnapshotIntervalMs?: number;
maxEventsPerChunk?: number;
maxPayloadBytes?: number;
};
+// A full snapshot copies the whole page, so how often one is taken decides
+// most of a recording's size; it must not follow the flush interval. Same
+// interval as PostHog. rrweb only checks it when it records an event, so an
+// idle page takes no extra snapshots.
+const DEFAULT_FULL_SNAPSHOT_INTERVAL_MS = 5 * 60 * 1000;
+
export type ReplayChunkPayload = {
chunk_index: number;
events_count: number;
@@ -44,6 +51,8 @@ export function startReplayRecorder(
const maxEventsPerChunk = config.maxEventsPerChunk ?? 200;
const flushIntervalMs = config.flushIntervalMs ?? 10_000;
+ const fullSnapshotIntervalMs =
+ config.fullSnapshotIntervalMs ?? DEFAULT_FULL_SNAPSHOT_INTERVAL_MS;
const maxPayloadBytes = config.maxPayloadBytes ?? 1_048_576; // 1MB
let buffer: eventWithTime[] = [];
@@ -115,7 +124,7 @@ export function startReplayRecorder(
buffer.push(event);
flushIfNeeded(!!isCheckout);
},
- checkoutEveryNms: flushIntervalMs,
+ checkoutEveryNms: fullSnapshotIntervalMs,
maskAllInputs: config.maskAllInputs ?? true,
maskTextSelector: maskAllText ? '*' : '[data-openpanel-replay-mask]',
maskTextFn: