feat(leaf): preserve keyed state and stream Suspense fallbacks
Problem
Leaf matches siblings by position. Reordering a list can move component state to a different item. A suspended child can also delay ready siblings in a streamed response.
Change
Match keyed children by sibling key, apply host refs after DOM changes, and reveal Suspense boundaries with a fallback as they resolve. The streaming route uses the request nonce for its reveal scripts and sends resolved markup on client navigation. The toggle example connects its primitive to the host button.
Safety and compatibility
Unkeyed lists keep positional matching. Boundaries without a fallback stream in document order. This change builds on #5 and stays within Leaf's runtime and adapter example.
Verification
Ran on macOS against the exact combined tree of this branch and the squash merge of #5, using Node 24 and Chromium.
pnpm install --frozen-lockfileandpnpm type-checkpassed.pnpm testpassed all 66 tests, including DOM identity during keyed reorder and ref lifecycle.pnpm test:e2e:devandpnpm test:e2epassed all 22 tests in each mode, including out-of-order sibling completion with a content security policy nonce.
Documentation and release
Updates the README, the rendering decision record, and the runnable streaming and toggle examples. The workspace packages are private.
Summary by cubic
Keyed siblings now match by key instead of position, so reordering a list keeps each item's DOM node and state. Host refs attach after the DOM update and clear when a node is replaced or removed.
Suspense boundaries with an explicit fallback stream independently: a later sibling can finish first, and a small inline script reveals each completed boundary in place using the response nonce when supplied. progressive: false returns resolved, scriptless HTML for consumers that don't execute scripts.
Details
- Unkeyed siblings keep positional matching; duplicate keys throw at render.
- The streaming example supplies its fallback only on document loads and varies the response by
Sec-Fetch-Dest. - The toggle example now connects ui-sdk's primitive to the host button through a ref.