Skip to content

armgit / fuobe

Public Imported from GitHub
0

The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.

Sidebar prefetches only the rows next to the open page when idle

Adarsh719109ee7a77
diff --git a/src/components/app-sidebar.tsx b/src/components/app-sidebar.tsx
index b20ba1bb..c9091163 100644
--- a/src/components/app-sidebar.tsx
+++ b/src/components/app-sidebar.tsx
@@ -104,12 +104,17 @@ function TappedRowReset() {
return null;
}
+/** How many rows above and below the open page's row prefetch once the page is idle (the rest wait for a hover, touch or focus). */
+const NEAR_ROWS = 2;
+
function Items({ group, pathname, search }: { group: NavGroupDef; pathname: string; search: URLSearchParams }) {
const hrefs = group.items.map((i) => i.href);
+ // The open page's row: the rows next to it are the likeliest next taps.
+ const here = group.items.findIndex((i) => navActive(i.href, pathname, search, hrefs));
const tapped = useSyncExternalStore(subscribeTapped, () => tappedRow, () => null);
const [at, query] = tapped ? tapped.split("?") : [pathname];
const where = tapped ? new URLSearchParams(query ?? "") : search;
- return group.items.map((item) => {
+ return group.items.map((item, i) => {
const active = navActive(item.href, at, where, hrefs);
return (
<SidebarMenuItem key={item.href} className={cn(group.compact && "min-w-0")}>
@@ -125,7 +130,7 @@ function Items({ group, pathname, search }: { group: NavGroupDef; pathname: stri
<IntentLink
href={item.href}
eager={item.eager}
- settled
+ settled={here >= 0 && i !== here && Math.abs(i - here) <= NEAR_ROWS}
onClick={(e) => isPlainClick(e) && setTappedRow(item.href)}
aria-current={active ? "page" : undefined}
title={group.compact && item.short ? item.label : undefined}
diff --git a/src/components/intent-link.tsx b/src/components/intent-link.tsx
index f790888b..0ed77933 100644
--- a/src/components/intent-link.tsx
+++ b/src/components/intent-link.tsx
@@ -12,10 +12,9 @@ import { cn } from "@/lib/utils";
* once the pointer is over it, a finger touches it or it gets keyboard focus (the "hover-triggered
* prefetch" of Next's prefetching guide). `eager` rows prefetch as soon as they're on screen, like a
* plain Link: the few routes most visits go to next. `settled` rows prefetch once the page has
- * loaded and the browser is idle, also the ones scrolled out of view (the app's own navigation: a tap
- * there must not wait a round trip, and a touch comes too late to start one), after the page's own
- * tabs (useIdlePrefetch's "nav" lane). A page full of these no longer fetches every route behind it
- * on a cold visit.
+ * loaded and the browser is idle, also when scrolled out of view, after the page's own tabs
+ * (useIdlePrefetch's "nav" lane): the sidebar gives it to the few rows next to the open page's row.
+ * A page full of these fetches only those routes on a cold visit, not every route behind it.
*/
export function IntentLink({
eager,
diff --git a/src/lib/idle-prefetch.ts b/src/lib/idle-prefetch.ts
index 116340b6..0e45851f 100644
--- a/src/lib/idle-prefetch.ts
+++ b/src/lib/idle-prefetch.ts
@@ -2,7 +2,8 @@
import { useRouter } from "next/navigation";
import { useEffect } from "react";
-import type { PrefetchOptions } from "next/dist/shared/lib/app-router-context.shared-runtime";
+
+type PrefetchOptions = NonNullable<Parameters<ReturnType<typeof useRouter>["prefetch"]>[1]>;
/** Runs `fn` once the browser is idle after the page has loaded (soon after, where requestIdleCallback is missing). Returns a cancel. */
export function whenIdle(fn: () => void): () => void {
@@ -77,6 +78,10 @@ export function afterPrefetched(hrefs: string[], done: () => void, quiet = 500,
check();
}
+function saveData(): boolean {
+ return typeof navigator !== "undefined" && !!(navigator as Navigator & { connection?: { saveData?: boolean } }).connection?.saveData;
+}
+
function flush() {
flushing = false;
const page = [...lanes.page];
@@ -92,6 +97,8 @@ function flush() {
/** Queues one prefetch of `href` (`run`) for the next idle moment, in its lane. Returns a cancel. */
export function queueIdlePrefetch(lane: PrefetchLane, href: string, run: () => void): () => void {
+ // Someone who asked to save data gets the page's own tabs only; the app's routes wait for a hover, touch or focus.
+ if (lane === "nav" && saveData()) return () => {};
const job = { href, run };
lanes[lane].add(job);
if (!flushing) {
@@ -103,7 +110,9 @@ export function queueIdlePrefetch(lane: PrefetchLane, href: string, run: () => v
/**
* router.prefetch's options. `full`: what <Link prefetch={true}> asks for, the whole page with its URL data (a prerendered
- * page comes from the static cache), rather than only the route's shared shell.
+ * page comes from the static cache), rather than only the route's shared shell. `kind` isn't in Next's documented
+ * options (only onInvalidate is) and an unknown value falls back to the shell without an error: tests/instant-tabs.test.ts
+ * checks that Next still reads it.
*/
export function prefetchOptions(full: boolean, onInvalidate?: () => void): PrefetchOptions {
return { kind: full ? "full" : "auto", onInvalidate } as unknown as PrefetchOptions;