Skip to content

Add a first-class behavior-only root and external DOM ownership lifecycle #650

Closed

Description

@domgan-oai

Summary

Add a public, documented lifecycle for attaching behavior to server-rendered DOM without creating a reconciling component root, while interoperating with independently owned DOM ranges and streamed updates.

This is a focused follow-up to the already-closed #211 (JS-09) and #228 (external ownership and deferred streaming). The permanent-static boundary work in #229 already preserves externally updated DOM; this issue is about the remaining behavior-adoption, ownership-conflict, readiness, and disposal contract.

Existing capability and remaining gap

<Hydrate split={false} when={never()}> already preserves DOM identity, avoids a persistent wrapper, and prevents reconciliation of its descendants. The existing external-mutation regression explicitly distinguishes that guarantee from a behavior-adoption, conflict-resolution, or disposal API.

The same static boundary also intentionally erases its descendant client graph, so component event handlers inside that range are not installed. Adding additional lifecycle props changes the exact permanent-static optimization, while ordinary hydration installs behavior by also taking component/reconciliation ownership. Root disposal currently clears the container rather than offering explicit retention semantics.

There is no supported public contract for combining:

  • externally owned markup that must retain node identity;
  • behavior modules that attach to that markup without rendering it;
  • independently streamed DOM mutations before, during, or after attachment; and
  • deterministic readiness, ownership, conflict handling, and cleanup.

Minimal reproduction

import { Hydrate } from 'octane';
import { never } from 'octane/hydration';

export function StreamedPage(props: {
  initialText: string;
  onAction: () => void;
}) @{
  <main id="app">
    <Hydrate split={false} when={never()}>
      <section id="externally-owned">
        <button type="button" onClick={props.onAction}>Action</button>
        <article>{props.initialText}</article>
      </section>
    </Hydrate>
    <span data-live-label>Ready</span>
  </main>
}
  1. Render StreamedPage with renderToString or renderToReadableStream.
  2. Append or replace children beneath #externally-owned using an independent DOM updater driven by a Web ReadableStream.
  3. Hydrate the surrounding root: the externally updated nodes are correctly preserved.
  4. Observe that the nested action handler is not installed because the permanently static descendant graph is intentionally removed.
  5. Replace the static boundary with ordinary hydration: behavior attaches, but the framework now owns reconciliation of the independently updated range.

The missing capability is a first-class way to preserve the external ownership guarantee while attaching narrowly scoped behavior and managing its complete lifecycle.

Proposed direction

The exact API shape should be maintainer-defined. One possible model is an explicit behavior-only root that accepts external range registrations and disposable behavior entries:

const owner = attachBehaviorRoot(document.querySelector('#app')!, {
  signal: pageLifetime.signal,
});

owner.registerExternalRange(document.querySelector('#externally-owned')!, {
  owner: streamOwner,
  ready: streamSettled,
});

owner.registerBehavior({
  target: '#externally-owned button',
  adopt(element, { signal }) {
    element.addEventListener('click', onAction, { signal });
  },
});

owner.dispose({ preserveDOM: true });

This snippet illustrates lifecycle boundaries, not a required API or existing exported function.

Acceptance criteria

  • Attach behavior to existing SSR markup without replacing nodes, rerendering the document, mutating protected attributes, or claiming reconciliation ownership of externally managed descendants.
  • Preserve external DOM mutations made before attachment, during streamed insertion or reveal, and after attachment; support late-registered and nested ranges with a deterministic policy for overlapping owners.
  • Support interaction arriving before an asynchronously loaded behavior module is ready. Preserve the original same-document event and its genuine trust/provenance; never synthesize trusted events, bypass transient user-activation rules, or perform native link/form activation twice.
  • Allow delegated behavior to discover subsequently inserted nodes without claiming the external render graph.
  • Provide explicit attach, readiness, cancellation, conflict, handoff, and disposal semantics; run cleanup exactly once and ignore stale asynchronous work after an owner is aborted or replaced.
  • Make document/root replacement explicit, including document-scoped renderer identity, preservation versus removal of externally owned DOM, and cleanup of old listeners, observers, and range registrations.
  • Preserve existing permanent-static optimizations: no wrapper, no accidental client import of server-only components, stable hydration IDs, and correct HTML/SVG/MathML namespaces.
  • Work with Node-based buffered SSR, standards-based ReadableStream SSR, browser AbortSignal, and deferred hydration without requiring Node-specific globals in browser bundles.
  • Add Chromium and WebKit coverage for progressively streamed text, annotation links, embedded widgets, nested ownership ranges, first-interaction adoption, repeated form interactions, aborted streams, and document replacement.

Non-goals

  • Regressing or replacing the existing permanent-static boundary implementation.
  • Claiming that existing static-range DOM preservation is broken.
  • Treating synthetic events as trusted user interactions.
  • Forcing the server-rendered component graph into the behavior-only client bundle.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions