: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): fall back when the player frame never loads (#100643)
posthog[bot]bafce1ad01f5
diff --git a/frontend/src/scenes/session-recordings/player/PlayerFrame.test.tsx b/frontend/src/scenes/session-recordings/player/PlayerFrame.test.tsxindex ff7f89b2c842..835b9fd7b36b 100644--- a/frontend/src/scenes/session-recordings/player/PlayerFrame.test.tsx+++ b/frontend/src/scenes/session-recordings/player/PlayerFrame.test.tsx@@ -53,6 +53,62 @@ describe('PlayerFrame', () => {expect(iframe).toHaveAttribute('sandbox', 'allow-same-origin')})+ // A load event that never arrives used to leave rrweb with nowhere to mount, so the player+ // showed an empty rectangle for as long as the tab stayed open. The timeout feeds the same+ // failure action as a load without a mount node, so the first timeout retries the frame and+ // the retry flow (or the offline shortcut) reaches the app-document fallback.+ it('retries the frame when the load event never arrives, then falls back', () => {+ jest.useFakeTimers()+ try {+ const captureSpy = jest.spyOn(posthog, 'capture')+ const captureExceptionSpy = jest.spyOn(posthog, 'captureException')+ const { container, iframe } = renderPlayerFrame()++ // The load never fires. The 10s timeout reports the first failure, which schedules a retry.+ act(() => {+ jest.advanceTimersByTime(10000)+ })+ expect(container.querySelector('iframe')).toBe(iframe)+ expect(captureSpy).toHaveBeenCalledWith(+ 'replay player frame load retried',+ expect.objectContaining({ attempt: 1 })+ )++ // The retry changes the src, and the reloaded frame's load also never fires. The frame+ // gets MAX_PLAYER_FRAME_LOAD_RETRIES retries, so failures 1 and 2 retry and failure 3+ // takes the app-document fallback.+ act(() => {+ jest.advanceTimersByTime(1000)+ })+ expect(iframe).toHaveAttribute('src', '/replay_player_frame/index.html?retry=1')++ act(() => {+ jest.advanceTimersByTime(10000)+ })+ expect(captureSpy).toHaveBeenCalledWith(+ 'replay player frame load retried',+ expect.objectContaining({ attempt: 2 })+ )++ act(() => {+ jest.advanceTimersByTime(2000)+ })+ expect(iframe).toHaveAttribute('src', '/replay_player_frame/index.html?retry=2')++ act(() => {+ jest.advanceTimersByTime(10000)+ })++ expect(container.querySelector('iframe')).toBeNull()+ const fallback = container.querySelector('div.PlayerFrame__content')+ expect(fallback).not.toBeNull()+ expect(sessionRecordingPlayerLogic(logicProps).values.rootFrame).toBe(fallback)+ expect(captureExceptionSpy).toHaveBeenCalledTimes(1)+ } finally {+ jest.useRealTimers()+ }+ })+// Firefox fires load for the frame's initial about:blank document, which has no mount node.// The shell document is still on its way, so the frame must keep its chance to load.it('keeps the frame and reports nothing when the blank first document loads', () => {diff --git a/frontend/src/scenes/session-recordings/player/PlayerFrame.tsx b/frontend/src/scenes/session-recordings/player/PlayerFrame.tsxindex d559a1983d12..bd8308b0f183 100644--- a/frontend/src/scenes/session-recordings/player/PlayerFrame.tsx+++ b/frontend/src/scenes/session-recordings/player/PlayerFrame.tsx@@ -20,6 +20,9 @@ const BASE_CLICK_INDICATOR_DURATION_S = 1 / 3// than the app's. CSPMiddleware supplies it.const PLAYER_FRAME_SRC = '/replay_player_frame/index.html'const PLAYER_FRAME_CONTENT_ID = 'player-frame-content'+// Without a timeout, a frame load event that never arrives leaves rrweb with nowhere to mount and+// the player stays blank for as long as the tab is open.+const PLAYER_FRAME_LOAD_TIMEOUT_MS = 10000export const PlayerFrame = (): JSX.Element => {const replayDimensionRef = useRef<viewportResizeDimension>()@@ -118,6 +121,20 @@ export const PlayerFrame = (): JSX.Element => {setRootFrame(frameRef.current)}, [setRootFrame, applyFrameStyles, playerFrameDocumentLoadFailed])+ // frameSrc is a dependency so each retry, which swaps the frame's document, arms its own+ // timeout instead of reusing the one armed for the previous document.+ useEffect(() => {+ if (!ownDocument) {+ return+ }+ const timer = setTimeout(() => {+ if (!frameRef.current) {+ playerFrameDocumentLoadFailed(iframeRef.current)+ }+ }, PLAYER_FRAME_LOAD_TIMEOUT_MS)+ return () => clearTimeout(timer)+ }, [ownDocument, frameSrc, playerFrameDocumentLoadFailed])+// Need useEffect to populate replayer on component paint. Under the flag the frame may still be// loading, in which case handleFrameLoad does this instead.// ownDocument is a dependency because flags resolve after the first paint. A user who holds a@@ -175,6 +192,11 @@ export const PlayerFrame = (): JSX.Element => {onLoad={handleFrameLoad}title="Session replay player"// Interaction belongs to the app's controls, not the recorded page.+ // Do not add allow-scripts even though the browser reports it missing. That+ // report is from rrweb's own replay frame, which carries the same sandbox and+ // cannot inherit a permission it does not request, so it changes nothing a+ // recording renders. It only lets this document, same-origin with the app, drop+ // its own sandbox.sandbox="allow-same-origin"/>) : (