Skip to content

More issues #228

Closed
Closed

Description

@domgan-oai

Summary

There are three closely related gaps when combining deferred hydration with streamed or externally updated DOM:

  1. a permanently static <Hydrate split={false} when={never()}> boundary retains an observable wrapper and can retain an unnecessary client subtree;
  2. a deferred boundary can activate while a nested streamed-Suspense reveal is still pending, causing the fallback to be adopted before the reveal arrives;
  3. applications that independently update a server-rendered DOM range need an explicit ownership/adoption lifecycle.

The examples below use generic components and an independent streaming updater.

Tested against [email protected] with @tsrx/[email protected].

1. Permanently static boundaries should preserve DOM shape and avoid the client subtree

Reproduction

"use octane";

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

export function App() @{
  <main>
    <Hydrate split={false} when={never()}>
      <aside class="sidebar">
        <StaticNavigation />
      </aside>
    </Hydrate>
  </main>
}

function StaticNavigation() @{
  <nav>
    <a href="/">Home</a>
    <a href="/about">About</a>
  </nav>
}

The intent is to render the navigation on the server, preserve it permanently, and avoid loading or executing its client graph.

An ordinary deferred-hydration wrapper changes the observable relationship from:

<main>
  <aside class="sidebar">...</aside>
</main>

to a shape with an intermediate element. This can break direct-child selectors/layout assumptions and is invalid or namespace-sensitive for foreign content such as SVG or MathML.

Current workaround

Special-case the exact static form during compilation/SSR, render its children directly, and leave only a renderer-private ID-count marker if required for hydration ID alignment:

<main>
  <!--octane-static-hydrate:N-->
  <aside class="sidebar">...</aside>
</main>

The client consumes/reserves the marker range but does not evaluate the static subtree.

Useful cases to cover:

// Multiple children.
<Hydrate split={false} when={never()}>
  <span>A</span>
  <span>B</span>
</Hydrate>

// Nested static boundaries.
<Hydrate split={false} when={never()}>
  <section>
    <Hydrate split={false} when={never()}>
      <span>Static</span>
    </Hydrate>
  </section>x
</Hydrate>

// Foreign content.
<svg viewBox="0 0 20 20">
  <Hydrate split={false} when={never()}>
    <g><path d="M0 0h20v20H0z" /></g>
  </Hydrate>
</svg>

Expected properties:

  • no persistent wrapper for the exact permanent-static form;
  • no client evaluation/import of the static descendants;
  • stable IDs for later hydrated siblings;
  • correct HTML/SVG/MathML namespaces;
  • nested static boundaries collapse safely.

2. Deferred hydration should not adopt a pending streamed-Suspense fallback

Reproduction shape

"use octane";

import { Hydrate, Suspense } from "octane";
import { interaction } from "octane/hydration";

export function App() @{
  <Hydrate when={interaction()}>
    <Suspense fallback={<button type="button">Loading...</button>}>
      <AsyncContent />
    </Suspense>
  </Hydrate>
}

The problematic ordering is:

1. SSR streams the Suspense fallback and leaves a pending reveal marker.
2. An interaction wakes the deferred hydration boundary.
3. Hydration adopts/claims the fallback range.
4. The server reveal arrives.
5. The reveal can no longer safely replace the claimed range.

The important case is activation after the fallback is parsed but before the streamed reveal has applied.

Current workaround

Before activating the boundary, detect unresolved renderer-owned reveal sentinels in that boundary and wait until each one reveals or degrades to client rendering:

async function activateBoundary(boundary: Element) {
  if (hasPendingRendererReveal(boundary)) {
    await waitForRendererRevealOrDegrade(boundary);
  }

  hydrateBoundary(boundary);
}

This needs a race-safe recheck after installing the observer because a reveal can apply between the initial scan and observer registration.

Ownership also matters for nesting:

<Hydrate when={interaction()}>
  <section>
    <Hydrate when={interaction()}>
      <Suspense fallback={<span>Loading...</span>}>
        <AsyncContent />
      </Suspense>
    </Hydrate>
  </section>
</Hydrate>

An outer boundary should not wait for a reveal owned by an independently dormant nested boundary; the nested boundary should coordinate its own activation.

A useful regression sequence would be:

const activation = activateDeferredBoundary();

// A renderer-owned reveal is still pending.
expect(isHydrated()).toBe(false);

// Simulate reveal or degradation.
removePendingRevealMarker();
// alternatively: markPendingRevealAsClientRendered();

await activation;

expect(isHydrated()).toBe(true);

Expected properties:

  • hydration waits only for renderer-owned pending reveals within the activating boundary;
  • reveal, degradation, cancellation, and observer-install races settle correctly;
  • nested deferred boundaries preserve ownership;
  • early interaction can still be replayed after activation.

3. Externally updated server DOM needs an explicit ownership/adoption lifecycle

Some applications stream an initial subtree through SSR and then update that subtree using a separate DOM-patching mechanism. Octane should render the initial range but must not later reconcile or replace descendants owned by that updater.

Reproduction shape

"use octane";

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

function ServerOwnedContent(props: {
  html: string;
}) @{
  <section class="content">
    <Hydrate split={false} when={never()}>
      <div
        data-server-owned-content=""
        dangerouslySetInnerHTML={{
          __html: props.html,
        }}
      />
    </Hydrate>
  </section>
}

After SSR, an independent updater may replace or append content under [data-server-owned-content]. A full-root or nearby hydration pass must leave that range intact.

Current workaround

  • put the externally updated subtree behind an exact permanent-static boundary;
  • keep a stable application-owned root for discovery;
  • omit the server-owned HTML from client hydration props so it is neither duplicated nor reconciled;
  • install behavior separately and make adoption/disposal idempotent.

Conceptually:

type BehaviorEntry = {
  adopt(
    root: Element | DocumentFragment,
    context: unknown,
  ): void | (() => void);
};

function installBehavior(
  entry: BehaviorEntry,
  root: Element,
  context: unknown,
) {
  const dispose = entry.adopt(root, context);

  return () => {
    dispose?.();
  };
}

An explicit API could take a different shape, for example <ServerOwned>, a behavior-only entry, or an adoptable range primitive. The useful guarantees are:

  • initial SSR without subsequent framework reconciliation of the owned range;
  • wrapper-free preservation where valid DOM shape matters;
  • behavior-only adoption without implying a client render;
  • idempotent adoption and deterministic disposal/replacement;
  • safe adoption of content inserted after the initial stream;
  • clear ordering/conflict semantics when multiple behaviors target the same range.

Suggested conformance matrix

  • static boundary with zero, one, and multiple children;
  • nested static and dynamic boundaries;
  • HTML, SVG, and MathML;
  • hydration IDs before/inside/after an opaque range;
  • streamed fallback followed by reveal, client-render degradation, or cancellation;
  • interaction before and during boundary activation;
  • externally replaced/appended content before and after hydration;
  • repeated adoption and disposal;
  • SSR/client production compilation and DOM-shape parity.

These are related correctness and lifecycle concerns, not a request to implicitly render ordinary expression statements in TSRX directive bodies.

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