:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(replay): recover a seek when the replay iframe has no head (#88074)
posthog[bot]53a44834e6a3
diff --git a/frontend/src/scenes/session-recordings/player/sessionRecordingDataCoordinatorLogic.ts b/frontend/src/scenes/session-recordings/player/sessionRecordingDataCoordinatorLogic.tsindex 42440a394e9e..e007cf72af8f 100644--- a/frontend/src/scenes/session-recordings/player/sessionRecordingDataCoordinatorLogic.ts+++ b/frontend/src/scenes/session-recordings/player/sessionRecordingDataCoordinatorLogic.ts@@ -803,8 +803,11 @@ export const sessionRecordingDataCoordinatorLogic = kea<sessionRecordingDataCoorconst result = { ...snapshotsByWindowId }for (const [windowId, ranges] of Object.entries(oversizedMutationRanges)) {result[windowId as unknown as number] = snapshotsByWindowId[windowId as unknown as number].filter(+ // Mutations only; a ViewportResize dropped here never comes back, since rrweb reads+ // dimensions from Meta rather than FullSnapshot(event) =>event.type !== EventType.IncrementalSnapshot ||+ event.data?.source !== IncrementalSource.Mutation ||!ranges.some((range) => event.timestamp >= range.start && event.timestamp < range.end))}diff --git a/frontend/src/scenes/session-recordings/player/sessionRecordingPlayerLogic.test.ts b/frontend/src/scenes/session-recordings/player/sessionRecordingPlayerLogic.test.tsindex 67035e02884e..3ed7a5a7b431 100644--- a/frontend/src/scenes/session-recordings/player/sessionRecordingPlayerLogic.test.ts+++ b/frontend/src/scenes/session-recordings/player/sessionRecordingPlayerLogic.test.ts@@ -952,6 +952,48 @@ describe('sessionRecordingPlayerLogic', () => {expect(logic.values.hasLateFullSnapshot).toBe(expectedHasLate)})++ // Builds a stand-in replayer whose iframe document has (or lacks) a <head>. rrweb throws+ // synchronously when it rebuilds a full snapshot on a document without a head, which is the+ // WebKit failure this recovery path guards against.+ const fakeReplayer = (head: HTMLElement | null): any => {+ const throwWhenHeadless = (): void => {+ if (!head) {+ throw new TypeError("null is not an object (evaluating 'doc.head.appendChild')")+ }+ }+ return {+ iframe: { contentDocument: { head } },+ play: jest.fn(throwWhenHeadless),+ pause: jest.fn(throwWhenHeadless),+ getCurrentTime: jest.fn(() => 0),+ setConfig: jest.fn(),+ on: jest.fn(),+ destroy: jest.fn(),+ service: { state: { context: { events: [] } } },+ }+ }++ it('re-inits the replayer instead of reporting a playback failure when the iframe has no head', async () => {+ seedRecording([fs(START), inc(START + 1000), inc(START + 11000)], [])+ logic.actions.setPause()++ const captureSpy = jest.spyOn(posthog, 'captureException')+ const tryInitReplayerSpy = jest.spyOn(logic.actions, 'tryInitReplayer')+ captureSpy.mockClear()+ tryInitReplayerSpy.mockClear()++ await expectLogic(logic, () => {+ logic.actions.setPlayer({ replayer: fakeReplayer(null), windowId: 1 })+ }).toFinishAllListeners()++ expect(tryInitReplayerSpy).toHaveBeenCalled()+ expect(captureSpy).not.toHaveBeenCalledWith(+ expect.anything(),+ expect.objectContaining({ feature: 'session-recording-replayer-playback' })+ )+ expect(logic.values.playerError).not.toBe('replayerPlaybackFailure')+ })})describe('delete session recording', () => {diff --git a/frontend/src/scenes/session-recordings/player/sessionRecordingPlayerLogic.ts b/frontend/src/scenes/session-recordings/player/sessionRecordingPlayerLogic.tsindex 292339bdf9e6..a7ca8bdf69b3 100644--- a/frontend/src/scenes/session-recordings/player/sessionRecordingPlayerLogic.ts+++ b/frontend/src/scenes/session-recordings/player/sessionRecordingPlayerLogic.ts@@ -134,6 +134,13 @@ export interface Player {windowId: number}+// WebKit can leave the replay iframe document without a <head> while rrweb rebuilds a full+// snapshot. rrweb then throws synchronously ("null is not an object") when it sets an attribute+// on the missing head. Detect that state so a seek can re-init the replayer instead of failing.+function isReplayerDocumentUnavailable(replayer: Replayer | undefined): boolean {+ return !!replayer && !replayer.iframe?.contentDocument?.head+}+export enum SessionRecordingPlayerMode {Standard = 'standard',Sharing = 'sharing',@@ -2435,6 +2442,27 @@ export const sessionRecordingPlayerLogic = kea<sessionRecordingPlayerLogicType>(// endBuffer first, so the pause/play decision below reads the user's play intent rather than the buffering state that outranks it.actions.endBuffer()actions.stopAnimation()++ // WebKit can leave the replay iframe without a <head>, so rrweb throws synchronously while+ // rebuilding a full snapshot. Re-init the replayer once so it rebuilds against a fresh+ // document, rather than a hard playback failure. The re-init seeks back here via setPlayer.+ const recoverStaleReplayer = (): boolean => {+ if (+ !isReplayerDocumentUnavailable(values.player?.replayer) ||+ (cache.replayerRecoveryAttempts ?? 0) >= 1+ ) {+ return false+ }+ cache.replayerRecoveryAttempts = (cache.replayerRecoveryAttempts ?? 0) + 1+ actions.tryInitReplayer()+ return true+ }++ // Guard before the seek, so rrweb never reaches the throw on a known-broken document.+ if (recoverStaleReplayer()) {+ return+ }+// rrweb throws synchronously on malformed events it replays through — surface an error// state rather than letting the throw escape the listener and wedge the state machine.try {@@ -2443,13 +2471,17 @@ export const sessionRecordingPlayerLogic = kea<sessionRecordingPlayerLogicType>(// in very large recordings this call to pause// can consume 100% CPU and freeze the entire pagevalues.player?.replayer?.pause(values.toRRWebPlayerTime(timestamp))- actions.clearPlayerError()} else {values.player?.replayer?.play(values.toRRWebPlayerTime(timestamp))actions.updateAnimation()- actions.clearPlayerError()}+ cache.replayerRecoveryAttempts = 0+ actions.clearPlayerError()} catch (error) {+ // The same failure can still slip through mid-play — recover rather than report it.+ if (recoverStaleReplayer()) {+ return+ }posthog.captureException(error, {feature: 'session-recording-replayer-playback',sessionRecordingId: props.sessionRecordingId,