Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

perf(web): stop preview loading rerenders (#7561)

Bilal Bakrbe7d35aaeb49
diff --git a/apps/web/src/components/preview/PreviewChromeRow.test.tsx b/apps/web/src/components/preview/PreviewChromeRow.test.tsx
index 77e13fb42..143d38e67 100644
--- a/apps/web/src/components/preview/PreviewChromeRow.test.tsx
+++ b/apps/web/src/components/preview/PreviewChromeRow.test.tsx
@@ -9,7 +9,6 @@ describe("PreviewChromeRow", () => {
<PreviewChromeRow
url="https://example.com/dashboard?mode=edit&tab=1#notes"
loading={false}
- loadProgress={0}
canGoBack={false}
canGoForward={false}
refreshDisabled={false}
diff --git a/apps/web/src/components/preview/PreviewChromeRow.tsx b/apps/web/src/components/preview/PreviewChromeRow.tsx
index 49f0b9fa1..d6a640842 100644
--- a/apps/web/src/components/preview/PreviewChromeRow.tsx
+++ b/apps/web/src/components/preview/PreviewChromeRow.tsx
@@ -24,7 +24,6 @@ import { cn } from "~/lib/utils";
interface Props {
url: string;
loading: boolean;
- loadProgress: number;
canGoBack: boolean;
canGoForward: boolean;
refreshDisabled: boolean;
@@ -65,7 +64,6 @@ const NOOP = () => {};
export function PreviewChromeRow({
url,
loading,
- loadProgress,
canGoBack,
canGoForward,
refreshDisabled,
@@ -308,16 +306,12 @@ export function PreviewChromeRow({
) : null}
{trailingActions}
</form>
- {loadProgress > 0 ? (
- <div
- aria-hidden
- className="pointer-events-none absolute bottom-0 left-0 z-10 h-0.5 rounded-r-full bg-primary transition-all duration-150 ease-out"
- style={{
- width: `${loadProgress}%`,
- boxShadow: "0 0 6px 1px var(--color-ring)",
- }}
- />
- ) : null}
+ <div
+ aria-hidden
+ data-loading={loading}
+ className="preview-loading-progress pointer-events-none absolute bottom-0 left-0 z-10 h-0.5 w-full origin-left rounded-r-full bg-primary"
+ style={{ boxShadow: "0 0 6px 1px var(--color-ring)" }}
+ />
</div>
);
}
diff --git a/apps/web/src/components/preview/PreviewView.test.tsx b/apps/web/src/components/preview/PreviewView.test.tsx
index 02f6277e4..2ace29ae5 100644
--- a/apps/web/src/components/preview/PreviewView.test.tsx
+++ b/apps/web/src/components/preview/PreviewView.test.tsx
@@ -5,6 +5,7 @@ import {
FILL_PREVIEW_VIEWPORT,
ThreadId,
} from "@t3tools/contracts";
+import { act, Profiler } from "react";
import { renderToStaticMarkup } from "react-dom/server";
import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
@@ -30,6 +31,7 @@ const mocks = vi.hoisted(() => ({
toggleAnnotation: null as (() => void) | null,
pictureInPicture: false,
showEmptyState: false,
+ loading: false,
recordVisitForThread: vi.fn(),
}));
@@ -103,7 +105,7 @@ vi.mock("~/previewStateStore", () => ({
hasWebContents: true,
canGoBack: false,
canGoForward: false,
- loading: false,
+ loading: mocks.loading,
zoomFactor: 1,
pictureInPicture: mocks.pictureInPicture,
colorScheme: "system",
@@ -244,7 +246,6 @@ vi.mock("./PreviewUnreachable", () => ({ PreviewUnreachable: () => null }));
vi.mock("./ZoomIndicator", () => ({ ZoomIndicator: () => null }));
vi.mock("./AgentBrowserCursor", () => ({ AgentBrowserCursor: () => null }));
vi.mock("~/browser/BrowserSurfaceSlot", () => ({ BrowserSurfaceSlot: () => null }));
-vi.mock("./useLoadingProgress", () => ({ useLoadingProgress: () => 0 }));
vi.mock("./usePreviewSession", () => ({ usePreviewSession: vi.fn() }));
import { PreviewView } from "./PreviewView";
@@ -256,6 +257,68 @@ const TEST_THREAD_REF = {
} as const;
const TEST_RUNTIME_TAB_ID = previewRuntimeTabId(TEST_THREAD_REF, null, "tab-1");
+// ReactDOM needs a host, but this unit suite intentionally has no DOM dependency.
+class TestNode {
+ parentNode: TestNode | null = null;
+ childNodes: TestNode[] = [];
+ readonly nodeName: string;
+ readonly tagName: string;
+ readonly namespaceURI = "http://www.w3.org/1999/xhtml";
+ readonly style = {};
+
+ constructor(
+ name: string,
+ readonly ownerDocument: TestNode | null = null,
+ readonly nodeType = 1,
+ ) {
+ this.nodeName = name.toUpperCase();
+ this.tagName = this.nodeName;
+ }
+
+ set textContent(_value: string) {
+ this.childNodes = [];
+ }
+
+ appendChild(child: TestNode) {
+ child.parentNode = this;
+ this.childNodes.push(child);
+ return child;
+ }
+
+ removeChild(child: TestNode) {
+ this.childNodes.splice(this.childNodes.indexOf(child), 1);
+ child.parentNode = null;
+ return child;
+ }
+
+ createElement(name: string) {
+ return new TestNode(name, this);
+ }
+
+ addEventListener() {}
+ removeEventListener() {}
+ setAttribute() {}
+}
+
+function installTestDom() {
+ const document = new TestNode("#document", null, 9);
+ const window = {
+ document,
+ HTMLIFrameElement: TestNode,
+ setInterval: globalThis.setInterval,
+ clearInterval: globalThis.clearInterval,
+ setTimeout: globalThis.setTimeout,
+ clearTimeout: globalThis.clearTimeout,
+ addEventListener() {},
+ removeEventListener() {},
+ };
+ vi.stubGlobal("document", document);
+ vi.stubGlobal("window", window);
+ vi.stubGlobal("HTMLIFrameElement", window.HTMLIFrameElement);
+ vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
+ return document;
+}
+
describe("PreviewView navigation", () => {
beforeEach(() => {
mocks.navigate.mockClear();
@@ -279,9 +342,38 @@ describe("PreviewView navigation", () => {
mocks.toggleAnnotation = null;
mocks.pictureInPicture = false;
mocks.showEmptyState = false;
+ mocks.loading = false;
mocks.recordVisitForThread.mockClear();
});
+ it("does not rerender while loading time passes", async () => {
+ vi.useFakeTimers();
+ mocks.loading = true;
+ const document = installTestDom();
+ const { createRoot } = await import("react-dom/client");
+ const root = createRoot(document.createElement("div") as unknown as Element);
+ const onRender = vi.fn();
+
+ try {
+ await act(() => {
+ root.render(
+ <Profiler id="preview" onRender={onRender}>
+ <PreviewView threadRef={TEST_THREAD_REF} tabId="tab-1" visible />
+ </Profiler>,
+ );
+ });
+ const initialRenderCount = onRender.mock.calls.length;
+
+ await act(() => vi.advanceTimersByTimeAsync(1_000));
+
+ expect(onRender).toHaveBeenCalledTimes(initialRenderCount);
+ } finally {
+ await act(() => root.unmount());
+ vi.useRealTimers();
+ vi.unstubAllGlobals();
+ }
+ });
+
it.each([
[
"https://localhost:8000/dashboard?mode=test#top",
diff --git a/apps/web/src/components/preview/PreviewView.tsx b/apps/web/src/components/preview/PreviewView.tsx
index 5a828b863..9a8129237 100644
--- a/apps/web/src/components/preview/PreviewView.tsx
+++ b/apps/web/src/components/preview/PreviewView.tsx
@@ -50,7 +50,6 @@ import { revealInFileExplorerLabel } from "./fileExplorerLabel";
import { shouldShowPreviewEmptyState } from "./previewEmptyStateLogic";
import { BrowserSurfaceSlot } from "~/browser/BrowserSurfaceSlot";
import { useBrowserSurfaceStore } from "~/browser/browserSurfaceStore";
-import { useLoadingProgress } from "./useLoadingProgress";
import { usePreviewSession } from "./usePreviewSession";
import { ZoomIndicator } from "./ZoomIndicator";
import { AgentBrowserCursor } from "./AgentBrowserCursor";
@@ -142,7 +141,6 @@ export function PreviewView({
const isUnreachable = navStatus._tag === "LoadFailed";
const showEmptyState = shouldShowPreviewEmptyState(snapshot);
const controller = desktopOverlay?.controller ?? "none";
- const loadProgress = useLoadingProgress(loading);
const viewport = snapshot?.viewport ?? FILL_PREVIEW_VIEWPORT;
const browserDefaults = useBrowserDefaults();
const panelRect = useBrowserSurfaceStore((state) =>
@@ -661,7 +659,6 @@ export function PreviewView({
<PreviewChromeRow
url={url}
loading={loading}
- loadProgress={loadProgress}
canGoBack={canGoBack}
canGoForward={canGoForward}
refreshDisabled={refreshDisabled}
diff --git a/apps/web/src/components/preview/useLoadingProgress.ts b/apps/web/src/components/preview/useLoadingProgress.ts
deleted file mode 100644
index 6c47cea60..000000000
--- a/apps/web/src/components/preview/useLoadingProgress.ts
+++ /dev/null
@@ -1,45 +0,0 @@
-import { useEffect, useRef, useState } from "react";
-
-const TICK_INTERVAL_MS = 120;
-const FADE_OUT_DELAY_MS = 220;
-const SEED_PERCENT = 4;
-const ASYMPTOTE_PERCENT = 90;
-const APPROACH_FACTOR = 0.08;
-const MIN_INCREMENT = 0.5;
-
-/**
- * Indeterminate progress simulator for the preview chrome's loading bar.
- * Animates 0 → 90% asymptotically while `loading` is true, snaps to 100%
- * on release, then resets after a short pause.
- *
- * Uses a ref to thread the latest progress through interval ticks without
- * needing `loading` to retrigger the effect, which sidesteps the stale-
- * closure pitfalls of reading `progress` directly.
- */
-export function useLoadingProgress(loading: boolean): number {
- const [progress, setProgress] = useState(0);
- const progressRef = useRef(0);
- progressRef.current = progress;
-
- useEffect(() => {
- if (!loading) {
- if (progressRef.current === 0) return;
- setProgress(100);
- const timer = window.setTimeout(() => setProgress(0), FADE_OUT_DELAY_MS);
- return () => window.clearTimeout(timer);
- }
-
- setProgress((value) => (value > 0 && value < 95 ? value : SEED_PERCENT));
- const interval = window.setInterval(() => {
- const current = progressRef.current;
- if (current >= ASYMPTOTE_PERCENT) return;
- const remaining = ASYMPTOTE_PERCENT - current;
- const increment = Math.max(MIN_INCREMENT, remaining * APPROACH_FACTOR);
- setProgress(Math.min(ASYMPTOTE_PERCENT, current + increment));
- }, TICK_INTERVAL_MS);
-
- return () => window.clearInterval(interval);
- }, [loading]);
-
- return progress;
-}
diff --git a/apps/web/src/index.css b/apps/web/src/index.css
index 71f9443f7..b602758d0 100644
--- a/apps/web/src/index.css
+++ b/apps/web/src/index.css
@@ -2357,3 +2357,38 @@ code {
.ultrathink-chroma {
animation: ultrathink-chroma-shift 10s linear infinite;
}
+
+@keyframes preview-loading-progress {
+ from {
+ transform: scaleX(0.04);
+ }
+
+ to {
+ transform: scaleX(0.9);
+ }
+}
+
+.preview-loading-progress {
+ opacity: 0;
+ transform: scaleX(1);
+ transition:
+ transform 150ms ease-out,
+ opacity 150ms ease-out 220ms;
+}
+
+.preview-loading-progress[data-loading="true"] {
+ opacity: 1;
+ animation: preview-loading-progress 5.3s cubic-bezier(0.1, 0.5, 0.2, 1) forwards;
+}
+
+@media (prefers-reduced-motion: reduce) {
+ .preview-loading-progress,
+ .preview-loading-progress[data-loading="true"] {
+ transition: none;
+ animation: none;
+ }
+
+ .preview-loading-progress[data-loading="true"] {
+ transform: scaleX(0.9);
+ }
+}