Skip to content

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

perf(web): disable sidebar viewport prefetch

XZHc7f46173cba1
diff --git a/web/components/sidebar/SidebarNav.tsx b/web/components/sidebar/SidebarNav.tsx
index ff9e0981d..f39e78e40 100644
--- a/web/components/sidebar/SidebarNav.tsx
+++ b/web/components/sidebar/SidebarNav.tsx
@@ -258,6 +258,7 @@ export function SidebarNav({
<Link
key={href}
href={href}
+ prefetch={false}
onClick={(event) => {
closeMenus();
if (href === "/chat") onHomeClick(event);
@@ -352,6 +353,7 @@ export function SidebarNav({
<Link
key={`${href}-destination`}
href={href}
+ prefetch={false}
draggable={false}
onClick={href === "/chat" ? onHomeClick : onNavigate}
className={`${rowClass} ${
@@ -553,6 +555,7 @@ function RailRow({
<Tooltip label={label} description={description} side="right">
<Link
href={href}
+ prefetch={false}
onClick={href === "/chat" ? onHomeClick : undefined}
aria-label={label}
className={`relative flex h-9 w-9 items-center justify-center rounded-xl transition-all duration-150 ${
diff --git a/web/components/sidebar/SidebarShell.tsx b/web/components/sidebar/SidebarShell.tsx
index dc2adbec9..3b0a82217 100644
--- a/web/components/sidebar/SidebarShell.tsx
+++ b/web/components/sidebar/SidebarShell.tsx
@@ -226,6 +226,7 @@ export function SidebarShell({
<div className="relative mb-2 flex h-9 w-9 items-center justify-center">
<Link
href="/"
+ prefetch={false}
aria-label="DeepTutor"
className="flex items-center justify-center transition-opacity duration-150 group-hover/sb:opacity-0"
>
@@ -264,6 +265,7 @@ export function SidebarShell({
<Link
key={item.href}
href={item.href}
+ prefetch={false}
title={t(item.label) as string}
className={`relative flex h-9 w-9 items-center justify-center rounded-xl transition-all duration-150 ${
active
@@ -302,7 +304,11 @@ export function SidebarShell({
<aside className="flex w-[220px] h-dvh shrink-0 flex-col bg-[var(--secondary)] transition-all duration-200">
{/* Header: logo + collapse toggle */}
<div className="flex h-14 items-center justify-between px-4">
- <Link href="/" className="group flex items-center gap-1.5">
+ <Link
+ href="/"
+ prefetch={false}
+ className="group flex items-center gap-1.5"
+ >
<Image
src="/logo.png"
alt="DeepTutor"
@@ -410,6 +416,7 @@ export function SidebarShell({
<Link
key={item.href}
href={item.href}
+ prefetch={false}
onClick={closeDrawerOnNav}
className={`flex items-center gap-2.5 rounded-lg px-3 py-2 text-[13.5px] transition-colors ${
active
diff --git a/web/components/sidebar/VersionBadge.tsx b/web/components/sidebar/VersionBadge.tsx
index e63735cc7..9ff86d607 100644
--- a/web/components/sidebar/VersionBadge.tsx
+++ b/web/components/sidebar/VersionBadge.tsx
@@ -76,6 +76,7 @@ export function VersionBadge({ collapsed = false }: VersionBadgeProps) {
return (
<Link
href="/settings#about"
+ prefetch={false}
scroll={false}
onClick={(event) => {
if (pathname !== "/settings") return;
diff --git a/web/tests/sidebar-prefetch.spec.tsx b/web/tests/sidebar-prefetch.spec.tsx
new file mode 100644
index 000000000..3db12880a
--- /dev/null
+++ b/web/tests/sidebar-prefetch.spec.tsx
@@ -0,0 +1,88 @@
+import { fireEvent, render, screen } from "@testing-library/react";
+import { expect, it, vi } from "vitest";
+import type { ComponentProps, ReactNode } from "react";
+
+import { SidebarShell } from "@/components/sidebar/SidebarShell";
+
+const fixture = vi.hoisted(() => ({
+ collapsed: false,
+ push: vi.fn(),
+ close: vi.fn(),
+ setCollapsed: vi.fn(),
+}));
+
+vi.mock("next/link", () => ({
+ default: ({
+ children,
+ prefetch,
+ ...props
+ }: Omit<ComponentProps<"a">, "prefetch"> & {
+ children?: ReactNode;
+ prefetch?: boolean;
+ }) => (
+ <a data-prefetch={String(prefetch)} {...props}>
+ {children}
+ </a>
+ ),
+}));
+vi.mock("next/navigation", () => ({
+ usePathname: () => "/chat",
+ useRouter: () => ({ push: fixture.push }),
+}));
+vi.mock("next/image", () => ({ default: () => null }));
+vi.mock("react-i18next", () => ({
+ useTranslation: () => ({ t: (key: string) => key, i18n: { language: "en" } }),
+}));
+vi.mock("@/context/AppShellContext", () => ({
+ useAppShell: () => ({
+ sidebarCollapsed: fixture.collapsed,
+ setSidebarCollapsed: fixture.setCollapsed,
+ }),
+}));
+vi.mock("@/components/access/CapabilityAccessContext", () => ({
+ useCapabilityAccess: () => ({ has: () => true }),
+}));
+vi.mock("@/components/layout/AppShell", () => ({
+ useSidebarDrawer: () => ({ close: fixture.close }),
+}));
+vi.mock("@/hooks/useDevice", () => ({
+ useDevice: () => ({ isMobile: false }),
+}));
+vi.mock("@/lib/app-update", () => ({
+ fetchAppUpdateStatus: vi.fn(() => Promise.resolve({ current_version: "1.0" })),
+ subscribeAppUpdateStatus: vi.fn(() => () => undefined),
+}));
+
+function expectSidebarLinksToDisablePrefetch() {
+ const links = [
+ ...document.querySelectorAll<HTMLAnchorElement>("a[data-prefetch]"),
+ ];
+ expect(links.length).toBeGreaterThan(0);
+ for (const link of links) {
+ expect(link).toHaveAttribute("data-prefetch", "false");
+ }
+}
+
+it("disables viewport prefetch for expanded sidebar navigation", async () => {
+ localStorage.setItem(
+ "deeptutor.sidebar.navLayout",
+ JSON.stringify({ order: [], collapsed: ["/books"] }),
+ );
+ fixture.collapsed = false;
+ render(<SidebarShell />);
+
+ fireEvent.click(await screen.findByRole("button", { name: /more/i }));
+ expectSidebarLinksToDisablePrefetch();
+});
+
+it("disables viewport prefetch for collapsed sidebar overflow navigation", async () => {
+ localStorage.setItem(
+ "deeptutor.sidebar.navLayout",
+ JSON.stringify({ order: [], collapsed: ["/books"] }),
+ );
+ fixture.collapsed = true;
+ render(<SidebarShell />);
+
+ fireEvent.click(await screen.findByRole("button", { name: /more/i }));
+ expectSidebarLinksToDisablePrefetch();
+});