The best way to control Claude Code, Codex and more from any computer or phone
perf(web): the hosted browser webview compiles with the React Compiler (#20558)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 16, 2026 at 22:07 UTC
095fdac93f6cb9b62bc21ed3d45bcde3ccbf7ce9
1 file changed
+50
-35
apps/web/src/browser/HostedBrowserWebview.tsx
+50
-35
@@ -6,14 +6,19 @@ import type {
6
ScopedThreadRef,
7
} from "@t3tools/contracts";
8
import { useShallow } from "zustand/react/shallow";
9
-import { useCallback, useEffect, useRef, useState } from "react";
9
+import { useEffect, useEffectEvent, useRef, useState } from "react";
10
11
import { previewBridge } from "~/components/preview/previewBridge";
12
import { usePreviewBridge } from "~/components/preview/usePreviewBridge";
13
import { useClientSettingsHydrated } from "~/hooks/useSettings";
14
import { cn, isMacPlatform } from "~/lib/utils";
15
16
-import { resolveBrowserSurfacePanelRect, useBrowserSurfaceStore } from "./browserSurfaceStore";
16
+import {
17
+ type BrowserSurfaceContentPresentation,
18
+ type BrowserSurfaceRect,
19
+ resolveBrowserSurfacePanelRect,
20
+ useBrowserSurfaceStore,
21
+} from "./browserSurfaceStore";
22
import { useActiveBrowserRecordingTabIds } from "./browserRecording";
23
import {
24
browserViewportSettingKey,
@@ -136,15 +141,14 @@ export function HostedBrowserWebview(props: {
141
142
const [webviewGeneration, setWebviewGeneration] = useState(0);
143
const [recoverySrc, setRecoverySrc] = useState(initialSrc);
139
- const latestUrlRef = useRef(initialUrl);
140
-
141
- useEffect(() => {
142
- latestUrlRef.current = initialUrl;
143
- }, [initialUrl]);
144
+ const remountCrashedWebview = useEffectEvent(() => {
145
+ setRecoverySrc(initialUrl ?? initialSrc);
146
+ setWebviewGeneration((generation) => generation + 1);
147
+ });
148
145
- const setWebviewRef = useCallback((node: HTMLElement | null) => {
149
+ const setWebviewRef = (node: HTMLElement | null) => {
150
webviewRef.current = node as ElectronWebview | null;
147
- }, []);
151
+ };
152
153
useEffect(() => {
154
const webview = webviewRef.current;
@@ -176,10 +180,7 @@ export function HostedBrowserWebview(props: {
180
crashRecoveryRef.current = recovery.state;
181
recoveryTimeout = setTimeout(() => {
182
recoveryTimeout = null;
179
- if (!disposed) {
180
- setRecoverySrc(latestUrlRef.current ?? initialSrc);
181
- setWebviewGeneration((generation) => generation + 1);
182
- }
183
+ if (!disposed) remountCrashedWebview();
184
}, recovery.delayMs);
185
};
186
// A click inside the guest only reaches this document as a webview focus
@@ -203,7 +204,13 @@ export function HostedBrowserWebview(props: {
204
webview.removeEventListener("render-process-gone", recoverGuest);
205
webview.removeEventListener("focus", dismissHostPopups);
206
};
206
- }, [clientSettingsHydrated, config, initialSrc, runtimeTabId, webviewGeneration]);
207
+ }, [
208
+ clientSettingsHydrated,
209
+ config,
210
+ runtimeTabId,
211
+ // oxlint-disable-next-line react/exhaustive-effect-dependencies -- A crash recovery remounts the webview under a new key, so the listeners attach to the new element.
212
+ webviewGeneration,
213
+ ]);
214
215
const active = presentation.visible && presentation.rect !== null;
216
const lastRect = presentation.rect;
@@ -214,25 +221,10 @@ export function HostedBrowserWebview(props: {
221
viewportWidth === null || viewportHeight === null ? null : viewportWidth / viewportHeight;
222
const lockedAspectRatio =
223
aspectRatioLocked && viewportAspectRatio !== null ? viewportAspectRatio : null;
217
- const handleAspectRatioChange = useCallback((aspectRatio: number | null) => {
224
+ const handleAspectRatioChange = (aspectRatio: number | null) => {
225
setAspectRatioLocked(aspectRatio !== null);
219
- }, []);
220
- const hiddenContentSize = presentation.content
221
- ? {
222
- width: presentation.content.width / presentation.content.scale,
223
- height: presentation.content.height / presentation.content.scale,
224
- }
225
- : null;
226
- const hiddenSize =
227
- viewport._tag !== "fill"
228
- ? {
229
- width: viewport.width * normalizedZoomFactor,
230
- height: viewport.height * normalizedZoomFactor,
231
- }
232
- : {
233
- width: hiddenContentSize?.width ?? lastRect?.width ?? 1280,
234
- height: hiddenContentSize?.height ?? lastRect?.height ?? 800,
235
- };
226
+ };
227
+ const hiddenSize = useHiddenSize(viewport, normalizedZoomFactor, presentation.content, lastRect);
228
const containerSize = active && lastRect ? lastRect : hiddenSize;
229
const deviceToolbarVisible = active && viewport._tag !== "fill" && !presentation.fitSourceContent;
230
const {
@@ -263,7 +255,7 @@ export function HostedBrowserWebview(props: {
255
? resolveBrowserViewportLayout(lastRect, fittedSourceViewport, normalizedZoomFactor)
256
: viewportLayout;
257
266
- const syncContentPresentation = useCallback(() => {
258
+ const syncContentPresentation = () => {
259
const wrapper = wrapperRef.current;
260
if (!wrapper) return;
261
useBrowserSurfaceStore.getState().presentContent(runtimeTabId, {
@@ -275,7 +267,7 @@ export function HostedBrowserWebview(props: {
267
scrollLeft: wrapper.scrollLeft,
268
scrollTop: wrapper.scrollTop,
269
});
278
- }, [layout, runtimeTabId]);
270
+ };
271
272
useEffect(() => {
273
const frameId = window.requestAnimationFrame(syncContentPresentation);
@@ -286,7 +278,13 @@ export function HostedBrowserWebview(props: {
278
const wrapper = wrapperRef.current;
279
if (!wrapper) return;
280
wrapper.scrollTo({ left: 0, top: 0 });
289
- }, [runtimeTabId, viewport._tag, viewportHeight, viewportWidth]);
281
+ }, [
282
+ // oxlint-disable-next-line react/exhaustive-effect-dependencies -- A new tab or viewport size starts the canvas scrolled to its origin.
283
+ runtimeTabId,
284
+ viewport._tag,
285
+ viewportHeight,
286
+ viewportWidth,
287
+ ]);
288
289
if (!clientSettingsHydrated || !config) return null;
290
@@ -392,3 +390,20 @@ export function HostedBrowserWebview(props: {
390
</div>
391
);
392
}
393
+
394
+// The size feeds useBrowserViewportResize, whose layout keys the content sync effect.
395
+function useHiddenSize(
396
+ viewport: PreviewViewportSetting,
397
+ zoomFactor: number,
398
+ content: BrowserSurfaceContentPresentation | null,
399
+ lastRect: BrowserSurfaceRect | null,
400
+) {
401
+ "use memo";
402
+ if (viewport._tag !== "fill") {
403
+ return { width: viewport.width * zoomFactor, height: viewport.height * zoomFactor };
404
+ }
405
+ return {
406
+ width: content ? content.width / content.scale : (lastRect?.width ?? 1280),
407
+ height: content ? content.height / content.scale : (lastRect?.height ?? 800),
408
+ };
409
+}