perf(core): hydrate a waiting layout root when a link in it is hovered

Merged
KI
15d ago
+89-1

Problem

In islands mode, the first navigation from a page under a layout that hydrates as one root is slower than RSC's. This is the shop and guide sections of benchmarks/server-components-payload. Measured with #3029 merged, after a 300 ms hover, those navigations took about twice as long as the next ones.

Root cause

The root's hydration only started on a click. When that click lands on a link, the router navigates before the root has hydrated. It cancels the pending hydration and renders the whole layout again in a new root. RSC hydrated the layout during load, so its first navigation only updates the page.

Change

When a layout root uses the interaction strategy, a link under the pointer inside it now starts the root's hydration, the same way a focus already did. The entry dispatches the existing farm:island-interaction event without holding anything. It also checks for a link that was already hovered when the entry ran, which is the hover that loaded it. The click then updates the hydrated root, as every later navigation does.

The production entry (nitro/universal-build.ts) and the dev entry (vite.ts) both get the change.

Safety and compatibility

  • Scope. Only roots of layouts that hydrate as one root and wait for a click are affected, and only in islands mode. Islands, load roots and other modes are unchanged.
  • Cost. A hover without a click now hydrates the root, which is the work RSC does on every load. Nothing hydrates without a pointer over a link or a focus.
  • Click during hydration. If the click comes before hydration has started, the existing path cancels it and renders a new root, as before.
  • API. There's no new option or public API.

Verification

First navigation after a cold visit, production builds of benchmarks/server-components-payload:

  • Chrome with 4x CPU throttling, 10 rounds, the three builds interleaved.
  • Medians in ms. "Before" is main with #3029.
SectionHoverRSCIslands, beforeIslands, after
shop100 ms95164143
guide100 ms160203181
blog (control)100 ms154151154
shop300 ms88169107
guide300 ms146187158

Islands is still a little slower than RSC on these first navigations. Navigations after the first are unchanged (shop 75 → 66 and guide 108 → 115 at 100 ms, within noise). The machine had other work running (load average 6–13).

  • New browser test, "a layout root that waits for a click hydrates once a link in it is hovered, and the click updates it". It checks that the root is hydrated after a hover and before any click, and that the click keeps the layout's nodes. It fails without the change, in production and in dev.
  • Islands production Playwright suite: 137 passed, 8 skipped.
  • Dev islands suite: 134 passed, 10 skipped, 1 failed. The failure is "a promise passed to a client component streams in, and the component works before it", and it also fails on main without this change on this machine (3 of 3 runs).
  • islands-client-entry, lazy-router-stand-in and hydration-server-props tests: 32 passed.
  • oxfmt and oxlint on the changed files: clean.

Not verified: touch devices, where there's no hover and the old path applies.

Documentation and release

docs/server-rendering says a waiting layout root hydrates when the pointer reaches one of its links.