Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

perf(web): load session viewer only on demand

Bingxi Zhao (Frank)6d893e96888a
diff --git a/web/components/chat/home/LazySessionViewerPanel.tsx b/web/components/chat/home/LazySessionViewerPanel.tsx
index 3a966d58f..84b466f11 100644
--- a/web/components/chat/home/LazySessionViewerPanel.tsx
+++ b/web/components/chat/home/LazySessionViewerPanel.tsx
@@ -6,6 +6,7 @@ import {
useCallback,
useImperativeHandle,
useRef,
+ useState,
} from "react";
import type {
SessionViewerPanelHandle,
@@ -14,8 +15,8 @@ import type {
export type { SessionViewerPanelHandle } from "./SessionViewerPanel";
-// Split the viewer's activity, follow-up, and consultation UI from the
-// initial chat and mastery route scripts while warming it on hydration.
+// The viewer's activity, follow-up, and consultation UI is only needed once
+// the panel opens or an event requests a tab.
const LoadedSessionViewerPanel = dynamic(
() => import("./SessionViewerPanel"),
{ ssr: false },
@@ -32,6 +33,7 @@ const LazySessionViewerPanel = forwardRef<
>(function LazySessionViewerPanel(props, ref) {
const panelRef = useRef<SessionViewerPanelHandle | null>(null);
const queuedCalls = useRef<QueuedCall[]>([]);
+ const [loadRequested, setLoadRequested] = useState(props.open);
const callPanel = useCallback(
(run: QueuedCall["run"]) => {
@@ -39,6 +41,7 @@ const LazySessionViewerPanel = forwardRef<
run(panelRef.current);
} else {
queuedCalls.current.push({ sessionId: props.sessionId, run });
+ setLoadRequested(true);
}
},
[props.sessionId],
@@ -80,7 +83,9 @@ const LazySessionViewerPanel = forwardRef<
[callPanel],
);
- return <LoadedSessionViewerPanel {...props} ref={setPanelRef} />;
+ return loadRequested || props.open ? (
+ <LoadedSessionViewerPanel {...props} ref={setPanelRef} />
+ ) : null;
});
export default LazySessionViewerPanel;
diff --git a/web/tests/lazy-session-viewer-panel.spec.tsx b/web/tests/lazy-session-viewer-panel.spec.tsx
index 2b79d3674..f1f3cf2f9 100644
--- a/web/tests/lazy-session-viewer-panel.spec.tsx
+++ b/web/tests/lazy-session-viewer-panel.spec.tsx
@@ -8,11 +8,12 @@ import type { SessionViewerPanelProps } from "@/components/chat/home/SessionView
const fixture = vi.hoisted(() => ({
ready: false,
+ mounts: 0,
calls: [] as string[],
}));
vi.mock("next/dynamic", async () => {
- const { forwardRef, useImperativeHandle } = await import("react");
+ const { forwardRef, useEffect, useImperativeHandle } = await import("react");
const LoadedPanel = forwardRef<SessionViewerPanelHandle>(function LoadedPanel(
_props,
ref,
@@ -32,6 +33,9 @@ vi.mock("next/dynamic", async () => {
return {
default: () =>
forwardRef<SessionViewerPanelHandle>(function DeferredPanel(_props, ref) {
+ useEffect(() => {
+ fixture.mounts += 1;
+ }, []);
return fixture.ready ? <LoadedPanel ref={ref} /> : null;
}),
};
@@ -40,9 +44,23 @@ vi.mock("next/dynamic", async () => {
afterEach(() => {
cleanup();
fixture.ready = false;
+ fixture.mounts = 0;
fixture.calls = [];
});
+it("does not mount the viewer while closed and idle", () => {
+ render(
+ <LazySessionViewerPanel
+ open={false}
+ sessionId="session-a"
+ activity={{} as SessionViewerPanelProps["activity"]}
+ onClose={() => {}}
+ onAutoOpen={() => {}}
+ />,
+ );
+ expect(fixture.mounts).toBe(0);
+});
+
it("replays viewer actions in order when the chunk loads", () => {
const ref = createRef<SessionViewerPanelHandle>();
const props = {
@@ -53,12 +71,14 @@ it("replays viewer actions in order when the chunk loads", () => {
onAutoOpen: () => {},
};
const view = render(<LazySessionViewerPanel {...props} ref={ref} />);
+ expect(fixture.mounts).toBe(0);
act(() => {
ref.current?.openWebTab("https://example.com");
ref.current?.focusActivityHome();
});
expect(fixture.calls).toEqual([]);
+ expect(fixture.mounts).toBeGreaterThan(0);
fixture.ready = true;
view.rerender(<LazySessionViewerPanel {...props} ref={ref} />);