Skip to content

: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.

fix(replay): keep recorded viewport width so Safari shows desktop layout (#91417)

posthog[bot]b6c1652a2633
diff --git a/frontend/src/scenes/session-recordings/player/PlayerFrame.tsx b/frontend/src/scenes/session-recordings/player/PlayerFrame.tsx
index aa66e6b018a0..24e64d16fd6a 100644
--- a/frontend/src/scenes/session-recordings/player/PlayerFrame.tsx
+++ b/frontend/src/scenes/session-recordings/player/PlayerFrame.tsx
@@ -7,6 +7,7 @@ import { useActions, useValues } from 'kea'
import { Handler, viewportResizeDimension } from 'posthog-js/rrweb-types'
import { useCallback, useEffect, useRef } from 'react'
+import { getPlayerFrameScale, isIOS } from 'scenes/session-recordings/player/playerFrameScaling'
import { sessionRecordingPlayerLogic } from 'scenes/session-recordings/player/sessionRecordingPlayerLogic'
const BASE_CLICK_INDICATOR_DURATION_S = 1 / 3
@@ -36,19 +37,19 @@ export const PlayerFrame = (): JSX.Element => {
const parentDimensions = frameRef.current.parentElement.getBoundingClientRect()
- const scale = Math.min(
- parentDimensions.width / dimensions.width,
- parentDimensions.height / dimensions.height,
- 1
- )
-
- // Scale with `zoom` instead of `transform: scale()`. A decimal transform scale
- // promotes the large replay iframe to a composited layer, which WebKit
- // re-rasterizes at pinch-zoom scale and crashes the tab on iOS (FB13816677).
- // `zoom` scales through layout, so no oversized layer exists. This also avoids
- // the Chrome GPU bug where an identity transform painted the iframe layer
- // outside its clipping bounds, which previously forced a 0.999 scale cap.
- player.replayer.wrapper.style.setProperty('zoom', String(scale))
+ const { scale, zoom, transform } = getPlayerFrameScale(parentDimensions, dimensions, isIOS())
+
+ const wrapperStyle = player.replayer.wrapper.style
+ if (zoom === null) {
+ wrapperStyle.removeProperty('zoom')
+ } else {
+ wrapperStyle.setProperty('zoom', zoom)
+ }
+ if (transform === null) {
+ wrapperStyle.removeProperty('transform')
+ } else {
+ wrapperStyle.setProperty('transform', transform)
+ }
setScale(scale)
},
diff --git a/frontend/src/scenes/session-recordings/player/playerFrameScaling.test.ts b/frontend/src/scenes/session-recordings/player/playerFrameScaling.test.ts
new file mode 100644
index 000000000000..60c651e6a44c
--- /dev/null
+++ b/frontend/src/scenes/session-recordings/player/playerFrameScaling.test.ts
@@ -0,0 +1,92 @@
+import {
+ getPlayerFrameScale,
+ isIOS,
+ PlayerFrameDimensions,
+ PlayerFrameScale,
+} from 'scenes/session-recordings/player/playerFrameScaling'
+
+const IPHONE =
+ 'Mozilla/5.0 (iPhone; CPU iPhone OS 18_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.1 Mobile/15E148 Safari/604.1'
+const IPAD_LEGACY =
+ 'Mozilla/5.0 (iPad; CPU OS 12_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1 Mobile/15E148 Safari/604.1'
+// iPadOS 13+ reports the same user agent as desktop Safari. Only maxTouchPoints separates them.
+const MAC_SAFARI =
+ 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.6 Safari/605.1.15'
+const MAC_CHROME =
+ 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36'
+const WINDOWS_CHROME =
+ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36'
+const ANDROID_CHROME =
+ 'Mozilla/5.0 (Linux; Android 14; Pixel 8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Mobile Safari/537.36'
+
+describe('isIOS', () => {
+ const original = { userAgent: navigator.userAgent, maxTouchPoints: navigator.maxTouchPoints }
+
+ function setNavigator(userAgent: string, maxTouchPoints: number): void {
+ Object.defineProperty(window.navigator, 'userAgent', { value: userAgent, configurable: true })
+ Object.defineProperty(window.navigator, 'maxTouchPoints', { value: maxTouchPoints, configurable: true })
+ }
+
+ afterEach(() => {
+ setNavigator(original.userAgent, original.maxTouchPoints)
+ })
+
+ it.each([
+ ['iPhone Safari', IPHONE, 5, true],
+ ['iPad on iPadOS 12', IPAD_LEGACY, 5, true],
+ ['iPad reporting a Macintosh user agent', MAC_SAFARI, 5, true],
+ ['macOS Safari', MAC_SAFARI, 0, false],
+ ['macOS Chrome', MAC_CHROME, 0, false],
+ ['Windows Chrome', WINDOWS_CHROME, 0, false],
+ ['Android Chrome', ANDROID_CHROME, 5, false],
+ ] as [string, string, number, boolean][])('%s', (_, userAgent, maxTouchPoints, expected) => {
+ setNavigator(userAgent, maxTouchPoints)
+
+ expect(isIOS()).toBe(expected)
+ })
+})
+
+describe('getPlayerFrameScale', () => {
+ it.each([
+ [
+ 'shrinks a desktop capture with transform off iOS',
+ { width: 855, height: 500 },
+ { width: 1710, height: 881 },
+ false,
+ { scale: 0.5, zoom: null, transform: 'scale(0.5)' },
+ ],
+ [
+ 'shrinks the same capture with zoom on iOS',
+ { width: 855, height: 500 },
+ { width: 1710, height: 881 },
+ true,
+ { scale: 0.5, zoom: '0.5', transform: null },
+ ],
+ [
+ 'drops the transform when no shrink is needed, so Chrome does not paint outside the clip',
+ { width: 1920, height: 1080 },
+ { width: 1710, height: 881 },
+ false,
+ { scale: 1, zoom: null, transform: null },
+ ],
+ [
+ 'still sets zoom at full size on iOS',
+ { width: 1920, height: 1080 },
+ { width: 1710, height: 881 },
+ true,
+ { scale: 1, zoom: '1', transform: null },
+ ],
+ [
+ 'picks the height ratio when that axis is the tighter fit',
+ { width: 1710, height: 440 },
+ { width: 1710, height: 880 },
+ false,
+ { scale: 0.5, zoom: null, transform: 'scale(0.5)' },
+ ],
+ ] as [string, PlayerFrameDimensions, PlayerFrameDimensions, boolean, PlayerFrameScale][])(
+ '%s',
+ (_, parent, recorded, onIOS, expected) => {
+ expect(getPlayerFrameScale(parent, recorded, onIOS)).toEqual(expected)
+ }
+ )
+})
diff --git a/frontend/src/scenes/session-recordings/player/playerFrameScaling.ts b/frontend/src/scenes/session-recordings/player/playerFrameScaling.ts
new file mode 100644
index 000000000000..0365904f5ee5
--- /dev/null
+++ b/frontend/src/scenes/session-recordings/player/playerFrameScaling.ts
@@ -0,0 +1,39 @@
+export interface PlayerFrameDimensions {
+ width: number
+ height: number
+}
+
+export interface PlayerFrameScale {
+ scale: number
+ // null clears the property on the replay wrapper.
+ zoom: string | null
+ transform: string | null
+}
+
+// A decimal `transform: scale()` crashes the tab on pinch-zoom in iOS WebKit (FB13816677).
+// iPadOS 13+ reports a Mac user agent; desktop Safari reports maxTouchPoints 0.
+export function isIOS(): boolean {
+ if (typeof navigator === 'undefined') {
+ return false
+ }
+ const ua = navigator.userAgent
+ return /iPad|iPhone|iPod/.test(ua) || (/Macintosh/.test(ua) && navigator.maxTouchPoints > 1)
+}
+
+export function getPlayerFrameScale(
+ parent: PlayerFrameDimensions,
+ recorded: PlayerFrameDimensions,
+ onIOS: boolean
+): PlayerFrameScale {
+ const scale = Math.min(parent.width / recorded.width, parent.height / recorded.height, 1)
+
+ if (onIOS) {
+ // `zoom` scales through layout, so no composited layer exists to re-rasterize.
+ return { scale, zoom: String(scale), transform: null }
+ }
+
+ // WebKit's `zoom` shrinks the iframe's documentElement.clientWidth, firing a desktop capture's
+ // mobile breakpoints. `transform` is visual only, so the recorded viewport survives. Chrome
+ // paints the iframe outside its clip bounds under an identity transform, so drop it at scale 1.
+ return { scale, zoom: null, transform: scale === 1 ? null : `scale(${scale})` }
+}