Repository navigation
fix(octane): recover when server HTML loses a hydration range's closing marker - #1753
Conversation
findMatchingClose assumed every server range closes in its parent and dereferenced null when a minifier or proxy had stripped the close. It now throws UnclosedHydrationRange, a native adoption miss: application boundaries pass it through and the root or a Hydrate boundary renders on the client and reports it as recoverable. A Suspense arm whose own range is intact renders on the client itself, so a hydration that finishes after the root commits recovers too. The binding-view root pair is matched inside the root attempt so its missing close recovers the same way. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
Bugbot Autofix is ON, but it could not run because the branch was deleted or merged before autofix could start.
Reviewed by Cursor Bugbot for commit 51a4cef. Configure here.
| journalRootChildren(parent); | ||
| previousHydration?.save(parent); | ||
| removeRange(getNextSibling(state.start), state.end); | ||
| initial.consume?.(); |
There was a problem hiding this comment.
Recovery drops streamed seeds early
Medium Severity
The new Suspense unclosed-range path calls initial.consume?.() and deletes initialSuspenseHydrations immediately. The catch-arm path defers that work through finishInitialSuspenseHydration until the attempt commits. A later rollback can restore the server arm's DOM, but the streamed payload is already gone from both initialStreamedPayloads and window.$OCTS, so the retry hydrates that arm without seeds.
Additional Locations (1)
Reviewed by Cursor Bugbot for commit 51a4cef. Configure here.


Summary
Hydration crashed when server HTML lost a range's closing
<!--]-->. Some HTML minifiers and proxies strip comments, so this can happen in production.findMatchingCloseassumed every range closes inside its parent. It walked off the end and threwTypeError: Cannot read properties of null (reading 'nodeType'). The root reported that throughonUncaughtErrorand emptied the container. When the missing close was a'use dom bindings'entry view's own root pair,hydrateRootthrew synchronously instead. The problem was found while working on #1743 (PR #1751), but it reproduces without early bindings: a plain component between static siblings is enough.Now the walk throws
UnclosedHydrationRangewhen the parent ends first. The nearest owner whose own range is intact renders on the client instead of adopting, and reports one recoverable error (#338):<Hydrate>boundary. It rolls back and mounts the island live. Content outside the boundary keeps its server nodes.root.render.Application
@catcharms andErrorBoundarys never receive the error.Why no local repair
I first tried to repair the DOM by closing the range at the end of its parent, the way an HTML parser closes an element whose end tag is missing. That stopped the crash, but the final DOM was wrong. The parent template adopts its
<section>wholesale, so the static<i>after the component is never claimed on its own. The extended range then discarded<i>as stale tail content.No guessed extent is safe. One that ends too late discards static template content after the range. One that ends too early leaves server nodes that no slot claims. So recovery goes to the nearest owner whose own range is trustworthy.
Design notes
UnclosedHydrationRangeextendsNativeAdoptionMiss. Every boundary already passes that error through viaisAdoptionControl, and the root and<Hydrate>owners already recover it by rolling back and rendering live. Subclassing reuses that path unchanged. Adding a separate class toisAdoptionControlwould have put it in every client bundle:cli-spa-startergrew 74 raw bytes when I tried it.journalRootChildrenplus the outer capability'ssave). If a later root-level suspension rolls the attempt back, the server arm is restored and the attempt that commits reports exactly once. A probe confirmed this.close()now runs inside the root attempt'stry, so a missing root close recovers like any other range instead of escapinghydrateRoot.docs/ssr.mdanddocs/differences-from-react.mdfirst: neither states a contract for malformed markers.docs/ssr.mdnow documents this behavior. It matches React's model, where a hydration error inside Suspense client-renders that boundary and otherwise the root.Validation
pnpm format:check: not run repo-wide.pnpm format:files:checkpasses on every changed file.pnpm typecheck: not run repo-wide.pnpm typecheck:filespasses onruntime.ts, the test, and the fixture.pnpm test: left to CI; local runs are limited while many sessions share this machine.packages/octane/tests/hydration/unclosed-range.test.ts(new): 50/50 inoctaneandoctane-prod, on the merge with main bda419a.'use dom bindings'component, a component in a@trybody, a'use dom bindings'entry view's root pair, and a Suspense arm whose hydration resumes after the root commits.<Hydrate>island.#338minified under the prod runtime), and final markup equal to the undamaged server render. It also checks that the client-rendered tree is live: clicks fire and updates apply in place.runtime.tsall 16 damaged cases fail (TypeError, a synchronous throw, or a timeout for the island) and the 9 controls pass. With only the Suspense branch removed, the 3 Suspense cases fail.--project octane:suspense-hydrate,initial-suspense-hydrate,early-control-handoff,range-boundary,deferred-hydration,suspended-clone-mismatch(108/108);scalar-dom-bindings,behavior-root(307/307). 73 of the latter go through the moved binding-view root block (checked by making that block throw).@ifarms, nested arms,@switch, keyed and unkeyed@forlists,ErrorBoundary, a@pendingarm in the same parent, and a root-level suspension before the damaged range. All recover with one report and correct DOM.pnpm error-codes:checkpasses.pnpm syncwas run and its CLI data output is committed.Bundle size. Measured with
run-minimal.mjson the merge with main bda419a:hydrate-rootraw / gzipdeferred-hydrationraw / gzipcli-spa-starter,root-static,suspense-transitionThe growth is hydration-only:
Both scenarios have about 32 bytes of headroom, so the raise goes in the separate budget-only PR #1754 per CONTRIBUTING "Size budgets". That PR merges first, then main merges into this one.
Risk / follow-ups
<!--]-->in the DOM without reporting. That cannot crash, but a stray close in a parent that holds an enclosing range could mis-terminate that range's walk. I left it out of scope here.NativeAdoptionMissthrown inside a Suspense arm's speculative attempt still goes to the root, as before. Only the unclosed-range subclass is recovered at the arm.Provenance
agent-authored)🤖 Generated with Claude Code
Need help on this PR? Tag
@codesmith-botwith what you need. Autofix is disabled.Note
Medium Risk
Touches core hydration cursor matching and multi-boundary recovery paths in
runtime.ts, but behavior is narrowly scoped to malformed marker HTML and reuses existingNativeAdoptionMissrollback semantics with new regression tests.Overview
Hydration no longer hard-fails when a server range is missing its
<!--]-->closing comment (e.g. after comment-stripping minifiers or proxies). Instead of walking past the parent and throwingCannot read properties of null (reading 'nodeType'),findMatchingClosethrowsUnclosedHydrationRange, which is treated like other adoption misses.Recovery re-renders on the client at the smallest trustworthy owner: the enclosing Suspense arm (including arms that finish hydrating after the root commits), a
<Hydrate>island, or the root—without surfacing the error to app@catch/ErrorBoundary. The damaged server slice is removed,onRecoverableErrorfires once with new client error #338, and the rest of the page can keep adopted server DOM where boundaries allow.hydrateRootnow matches binding-view root range closers inside the hydration attempt so a missing root close recovers instead of escaping synchronously. Docs (docs/ssr.md) and error catalog entries document the behavior; hydration tests cover plain/bindings/@try/Suspense/Hydrate scenarios.Reviewed by Cursor Bugbot for commit 51a4cef. Bugbot is set up for automated code reviews on this repo. Configure here.