Skip to content

πŸŽ₯ Make videos programmatically with React

`remotion`: Detect iPad Safari in desktop mode as iOS Safari

Victor Timsit3c628f0b36ec
diff --git a/packages/core/src/test/video-fragment.test.ts b/packages/core/src/test/video-fragment.test.ts
new file mode 100644
index 0000000000..ec3f1a4ccf
--- /dev/null
+++ b/packages/core/src/test/video-fragment.test.ts
@@ -0,0 +1,113 @@
+import {afterEach, describe, expect, test} from 'bun:test';
+import {appendVideoFragment, isIosSafari} from '../video/video-fragment.js';
+
+const IPHONE_SAFARI =
+ 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Mobile/15E148 Safari/604.1';
+// iPadOS 13+ with the default "Request Desktop Website" setting: the
+// user agent is indistinguishable from macOS Safari.
+const IPADOS_DESKTOP_MODE_SAFARI =
+ 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.2 Safari/605.1.15';
+const MAC_SAFARI = IPADOS_DESKTOP_MODE_SAFARI;
+const MAC_CHROME =
+ 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36';
+
+const setNavigator = ({
+ userAgent,
+ platform,
+ maxTouchPoints,
+}: {
+ userAgent: string;
+ platform: string;
+ maxTouchPoints: number;
+}) => {
+ Object.defineProperty(window.navigator, 'userAgent', {
+ value: userAgent,
+ configurable: true,
+ });
+ Object.defineProperty(window.navigator, 'platform', {
+ value: platform,
+ configurable: true,
+ });
+ Object.defineProperty(window.navigator, 'maxTouchPoints', {
+ value: maxTouchPoints,
+ configurable: true,
+ });
+};
+
+describe('isIosSafari', () => {
+ afterEach(() => {
+ for (const key of ['userAgent', 'platform', 'maxTouchPoints']) {
+ // Restore the happy-dom prototype getters.
+ delete (window.navigator as unknown as Record<string, unknown>)[key];
+ }
+ });
+
+ test('iPhone Safari is iOS Safari', () => {
+ setNavigator({
+ userAgent: IPHONE_SAFARI,
+ platform: 'iPhone',
+ maxTouchPoints: 5,
+ });
+ expect(isIosSafari()).toBe(true);
+ });
+
+ test('iPad Safari in desktop mode (Macintosh user agent) is iOS Safari', () => {
+ setNavigator({
+ userAgent: IPADOS_DESKTOP_MODE_SAFARI,
+ platform: 'MacIntel',
+ maxTouchPoints: 5,
+ });
+ expect(isIosSafari()).toBe(true);
+ });
+
+ test('Desktop Safari on a Mac is not iOS Safari', () => {
+ setNavigator({
+ userAgent: MAC_SAFARI,
+ platform: 'MacIntel',
+ maxTouchPoints: 0,
+ });
+ expect(isIosSafari()).toBe(false);
+ });
+
+ test('Chrome on a Mac is not iOS Safari', () => {
+ setNavigator({
+ userAgent: MAC_CHROME,
+ platform: 'MacIntel',
+ maxTouchPoints: 0,
+ });
+ expect(isIosSafari()).toBe(false);
+ });
+
+ test('does not append a media fragment to blob URLs on iPad in desktop mode', () => {
+ setNavigator({
+ userAgent: IPADOS_DESKTOP_MODE_SAFARI,
+ platform: 'MacIntel',
+ maxTouchPoints: 5,
+ });
+ const blob = 'blob:https://example.com/0f5a3c1e';
+ expect(
+ appendVideoFragment({
+ actualSrc: blob,
+ actualFrom: -30,
+ duration: 60,
+ fps: 30,
+ }),
+ ).toBe(blob);
+ });
+
+ test('still appends a media fragment to blob URLs on desktop Safari', () => {
+ setNavigator({
+ userAgent: MAC_SAFARI,
+ platform: 'MacIntel',
+ maxTouchPoints: 0,
+ });
+ expect(
+ appendVideoFragment({
+ actualSrc: 'blob:https://example.com/0f5a3c1e',
+ actualFrom: -30,
+ duration: 60,
+ fps: 30,
+ }),
+ ).toBe('blob:https://example.com/0f5a3c1e#t=1,2');
+ });
+});
diff --git a/packages/core/src/video/video-fragment.ts b/packages/core/src/video/video-fragment.ts
index 31822da12f..ab64cf67ea 100644
--- a/packages/core/src/video/video-fragment.ts
+++ b/packages/core/src/video/video-fragment.ts
@@ -25,9 +25,19 @@ export const isIosSafari = () => {
return false;
}
- const isIpadIPodIPhone = /iP(ad|od|hone)/i.test(window.navigator.userAgent);
+ const {userAgent, platform, maxTouchPoints} = window.navigator;
- return isIpadIPodIPhone && isSafari();
+ const isIpadIPodIPhone = /iP(ad|od|hone)/i.test(userAgent);
+
+ // Since iPadOS 13, Safari on iPad defaults to "Request Desktop Website"
+ // and sends a macOS user agent ("Macintosh; Intel Mac OS X 10_15_7"),
+ // so the string above never matches. A Mac platform with a touch
+ // screen is the only remaining signal β€” real Macs report 0 touch
+ // points. Without this, iPads get the blob + media fragment
+ // combination that Mobile Safari refuses to play (#1655).
+ const isIpadOsDesktopMode = platform === 'MacIntel' && maxTouchPoints > 1;
+
+ return (isIpadIPodIPhone || isIpadOsDesktopMode) && isSafari();
};
// https://github.com/remotion-dev/remotion/issues/1655