Guessing the next click
ForesightJS predicts which link a visitor is about to click from the path of the cursor, and TanStack Router gives it something cheap to do with the guess. This site already guesses: one Speculation Rules entry prerenders a page once the pointer rests on its link. This page runs five strategies over the same 65 cursor paths to find out whether guessing earlier helps, and what each wrong guess costs. The predictor here is 965 bytes of plain JavaScript; the library ships about 32 KB.
What the site does today
The shell sends one rule in a Speculation-Rules header: prerender any same-origin link at eagerness moderate, minus six paths that can't produce a reusable document. Chrome starts a moderate prerender once the pointer has rested on a link for 200 ms, or at pointerdown if that comes first. In the 30 days to 2026-11-13 the speculation ledger counted 363 speculated documents and 123 activations, so about 1 prerender in 3 got used.
Five strategies
| arm | what triggers it | what it fetches |
|---|---|---|
| moderate (today) | pointer rests 200 ms, or pointerdown | the page |
| predictor | the cursor's path, projected 120 ms ahead, lands on the link | the page |
| prefetch tier | predictor fires; moderate still prerenders later | HTML early, the page at moderate |
| 50 ms hover | pointer rests 50 ms, or pointerdown | the page |
| combo | predictor fetches HTML; a 50 ms hover prerenders | HTML early, the page at 50 ms |
The four new arms share one trick. A script tags the chosen link with an attribute, and a second rule at eagerness immediate matches the tag:
{ "prerender": [{
"where": { "and": [ <the shell rule's where>, { "selector_matches": "[data-intent]" } ] },
"eagerness": "immediate" }] }Chrome then does the rest: it fetches, merges the speculation with the moderate one for the same URL, and drops it when the tag comes off. Each arm keeps at most 2 tags, the same cap Chrome puts on moderate. The 50 ms hover is the whole of this:
let timer = 0, over = null;
addEventListener("pointerover", (e) => {
const a = e.target.closest?.("a[href]");
if (a === over) return;
clearTimeout(timer);
over = a;
if (a) timer = setTimeout(() => mark(a), 50); // sets data-intent
});The predictor follows ForesightJS: 8 points of history, a straight line projected 120 ms ahead, and a hit test along it. ForesightJS clips that line against every registered element's rectangle, which means tracking each rectangle with observers. elementFromPoint asks the browser to hit-test instead, so nothing needs tracking:
// keep the last 8 pointer positions; velocity from the oldest to the newest
const [x0, y0, t0] = pts[0], [x1, y1, t1] = pts[pts.length - 1];
const vx = (x1 - x0) / (t1 - t0), vy = (y1 - y0) / (t1 - t0);
// walk the line to where the cursor will be in 120 ms, nearest point first
for (const f of [0, 1 / 3, 2 / 3, 1]) {
const a = document.elementFromPoint(x1 + vx * 120 * f, y1 + vy * 120 * f)?.closest("a[href]");
if (a) return mark(a); // sets data-foresight; an immediate rule matches it
}Try it
Move the cursor to one of the links below and click it. The dot marks where the predictor thinks the cursor will be in 120 ms. Nothing is fetched: the demo times each strategy's trigger against your click and draws the head start each would have had.
Waiting for a click. On a touch screen the only signal is the tap itself, so every strategy ties.
Results
Each trial loads the page in Chrome 157, picks a link with a seeded random number generator, and moves the cursor there along a minimum-jerk path with a slight bow and 1 px of jitter, timed by Fitts's law. The cursor rests 80 to 250 ms, then clicks. Every arm replays identical paths, so each comparison is paired: 40 trials on / and 25 on /garage. Chrome's Preload protocol events timestamp when each speculation starts.
| arm | HTML head start, ms | render head start, ms | wasted prerenders per click | wasted prefetches per click |
|---|---|---|---|---|
| moderate (today) | 87 / 105 | 87 / 104 | 0.05 / 0.04 | 0 / 0 |
| predictor | 352 / 380 | 352 / 380 | 0.53 / 0.84 | 0 / 0 |
| prefetch tier | 378 / 396 | 99 / 96 | 0.03 / 0.00 | 0.47 / 0.76 |
| 50 ms hover | 271 / 271 | 271 / 271 | 0.10 / 0.20 | 0 / 0 |
| combo | 360 / 390 | 263 / 278 | 0.13 / 0.32 | 0.42 / 0.52 |
Medians, / then /garage. No arm missed the clicked link in any trial. The predictor and prefetch-tier rows come from a first run, the others from a second on the same paths; moderate's medians moved 2 to 13 ms between the runs and the 50 ms hover's 5 to 6 ms, which sets the noise floor for every row.
Moderate's head start comes out about one click long because the cursor rarely rests 200 ms before a click, so pointerdown fires it. The 50 ms hover catches the slowdown at the end of the reach instead, and gains about 150 ms for 0.1 to 0.2 extra prerenders per click. The predictor gains about 230 ms more, and pays with roughly one wasted prerender for every two clicks. The prefetch tier splits the difference: the HTML leaves about 245 ms early while rendering waits for moderate, so its head start depends on what the page is waiting for. The combo stacks the two halves, and each keeps its own timing.
What a wrong guess costs
| page | HTML, KiB | whole page, KiB |
|---|---|---|
/ | 11.0 | 219.1 |
/about | 1.6 | 34.6 |
/writing | 3.8 | 40.3 |
/garage | 19.4 | 112.8 |
/lwe | 2.6 | 37.1 |
/reading | 1.5 | 55.1 |
/coffee | 3.5 | 38.0 |
/serendipity | 3.2 | 46.0 |
/around | 1.9 | 37.4 |
/dotfiles | 8.5 | 42.7 |
A wasted prefetch costs the HTML, a median of about 3 KiB on these pages. A wasted prerender costs the whole page plus its render. The page totals come from an empty cache, so they overstate a prerender's bytes, since nav.js and luna.css are usually cached already, and they leave out render CPU. Even so, a wrong prerender runs about 10 times the price of a wrong prefetch.
The debugger switches prerender off
Chrome 157 refuses to prerender while any DevTools Protocol session is attached to the page, Playwright's included, and says so with PrerenderingDisabledByDevTools. The speculation falls back to a prefetch, and the page's timing entry reads deliveryType: "navigational-prefetch" with activationStart 0, exactly like an ordinary prefetch. The results above therefore time when each speculation starts. A separate run checked readiness: schedule the prerender from page JavaScript, detach, let the page navigate on its own, then reattach to read the timing entry. A prerendered page reports "navigational-prefetch" too, so only a nonzero activationStart tells the two apart.
What TanStack contributes
TanStack Router preloads a route's code and loader data after 50 ms of hover (defaultPreloadDelay) and treats the result as fresh for 30 s (preloadStaleTime). A wrong guess there costs one JSON request, which is why pairing it with an eager predictor works. A wrong prerender costs a page. The prefetch tier and the combo carry the idea down to the platform: spend a few KiB on a weak signal, and save the render for a strong one. The 30 s window isn't copied here yet; removing a tag after 30 s would copy it.
What production still has to say
Lab paths are smoother than hands, and every lab trial ends in a click, which is why moderate wastes almost nothing here and leaves 2 prerenders in 3 unused in the ledger. The lab also can't say whether today's prerenders finish before the click. PR 1671 makes nav.js report how far each prerender got at activation: complete, interactive, early or prefetch. If most read complete, moderate already starts in time and every arm here only adds waste. A real share of early or interactive points at the 50 ms hover first, since it buys about 150 ms with the fewest extra prerenders.