Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

fix: stop re-seeding replay snapshots indefinitely

Blue Mousefe11b958f9fe
diff --git a/packages/tracker-js/src/Lib.ts b/packages/tracker-js/src/Lib.ts
index 1a1b01029..b8c90bd8f 100644
--- a/packages/tracker-js/src/Lib.ts
+++ b/packages/tracker-js/src/Lib.ts
@@ -331,8 +331,12 @@ const DEFAULT_SESSION_REPLAY_SLIM_DOM_OPTIONS = {
}
const RRWEB_EVENT_FULL_SNAPSHOT = 2
// Chunk indices are reserved server-side, so retrying with the same index is
-// idempotent — the backend dedupes on (replayId, chunkIndex).
+// idempotent - the backend dedupes on (replayId, chunkIndex).
const SESSION_REPLAY_CHUNK_RETRY_DELAYS_MS = [2_000, 5_000, 15_000]
+// Snapshots are the largest payload we send, so a hop that rejects big bodies
+// rejects every re-seed too. Cap the attempts rather than re-uploading a
+// multi-megabyte snapshot on a loop for the rest of the session.
+const SESSION_REPLAY_MAX_SNAPSHOT_RESEEDS = 3
const SESSION_REPLAY_ACTIVITY_EVENTS = [
'click',
'keydown',
@@ -932,6 +936,7 @@ export class Lib {
: null
let chunkIndex = started.nextChunkIndex
+ let snapshotReseeds = 0
let stopped = false
let events: RrwebEvent[] = []
let eventsByteLength = 0
@@ -963,8 +968,10 @@ export class Lib {
if (
!delivered &&
!stopped &&
+ snapshotReseeds < SESSION_REPLAY_MAX_SNAPSHOT_RESEEDS &&
chunk.some((event) => event.type === RRWEB_EVENT_FULL_SNAPSHOT)
) {
+ snapshotReseeds += 1
try {
record.takeFullSnapshot?.()
} catch {}
diff --git a/packages/tracker-js/tests/sessionReplay.test.ts b/packages/tracker-js/tests/sessionReplay.test.ts
index 66e5f24b1..48dc812a7 100644
--- a/packages/tracker-js/tests/sessionReplay.test.ts
+++ b/packages/tracker-js/tests/sessionReplay.test.ts
@@ -660,4 +660,33 @@ describe('Session replay tracking', () => {
await actions.stop()
delete (mockRrwebRecord as any).takeFullSnapshot
})
+
+ test('snapshot re-seeding gives up instead of looping forever', async () => {
+ const takeFullSnapshot = jest.fn()
+ ;(mockRrwebRecord as any).takeFullSnapshot = takeFullSnapshot
+ const { recordOptions } = usePackageRrweb()
+ const { init, startSessionReplay } = await loadTracker()
+
+ init(PROJECT_ID, { devMode: true })
+ const actions = await startSessionReplay({ flushIntervalMs: 60_000 })
+
+ // A hop that rejects large bodies rejects every re-seeded snapshot too.
+ fetchMock.mockImplementation((url) => {
+ if (String(url).includes('/session-replay/chunk')) {
+ return Promise.resolve({ ok: false, status: 400 })
+ }
+
+ return Promise.resolve({ ok: true })
+ })
+
+ for (let attempt = 0; attempt < 6; attempt++) {
+ recordOptions().emit({ type: 2, timestamp: 100 + attempt })
+ await actions.flush()
+ }
+
+ expect(takeFullSnapshot).toHaveBeenCalledTimes(3)
+
+ await actions.stop()
+ delete (mockRrwebRecord as any).takeFullSnapshot
+ })
})