@@ -37,7 +37,7 @@ import {
3737 type StoreSetter ,
3838 type RevealOrder ,
3939 createOwner ,
40- createRoot ,
40+ createRoot as coreRoot ,
4141 getContext ,
4242 setContext ,
4343 type Context
@@ -172,6 +172,18 @@ type SharedConfig = {
172172 * @internal
173173 */
174174 onHydrationEnd ?: ( callback : ( ) => void ) => void ;
175+ /**
176+ * Whether a render under the current owner is part of the claim in
177+ * progress. The root pass claims everything; a streamed boundary's resume
178+ * window claims only the subtree under that boundary. A write landing in
179+ * the window (the resumed content's `onSettled` reaching a signal above
180+ * the boundary, #3504) re-renders an already-hydrated region: that render
181+ * is a client render — fresh nodes, live inserts — not a claim against the
182+ * registry. Assigned by enableHydration(); absent means "claiming".
183+ *
184+ * @internal
185+ */
186+ isClaiming ?: ( ) => boolean ;
175187} ;
176188
177189/**
@@ -204,6 +216,19 @@ let _hydrationEndCallbacks: (() => void)[] | null = null;
204216let _pendingBoundaries = 0 ;
205217let _hydrationDone = false ;
206218let _snapshotRootOwner : Owner | null = null ;
219+ // The boundary owner whose resume window is open (null during a root pass,
220+ // which claims everything). Reached as `sharedConfig.isClaiming`.
221+ let _claimOwner : Owner | null = null ;
222+
223+ function isClaiming ( ) : boolean {
224+ if ( ! _claimOwner ) return true ;
225+ let owner : Owner | null = getOwner ( ) ;
226+ while ( owner ) {
227+ if ( owner === _claimOwner ) return true ;
228+ owner = owner . _parent ;
229+ }
230+ return false ;
231+ }
207232
208233function markTopLevelSnapshotScope ( ) {
209234 if ( _snapshotRootOwner ) return ;
@@ -267,6 +292,7 @@ let _doneValue = false;
267292// (no hydrate() import), the hydrated* functions and their dependencies
268293// (MockPromise, subFetch) are eliminated by the bundler.
269294
295+ let _createRoot : Function | undefined ;
270296let _createMemo : Function | undefined ;
271297let _createSignal : Function | undefined ;
272298let _createErrorBoundary : Function | undefined ;
@@ -899,7 +925,7 @@ export function materializeContainerTrace(marker: {
899925 // below): materialization runs at arg-read inside a reader's render
900926 // scope, and a version signal owned by that reader would be disposed by
901927 // its re-render while the memoized store lives on.
902- return createRoot ( ( ) => {
928+ return coreRoot ( ( ) => {
903929 const [ version , setVersion ] = coreSignal ( 0 ) ;
904930 // Subscribe before creating the projection: the buffered replay runs
905931 // synchronously inside on(), filling the queue the first compute
@@ -958,7 +984,7 @@ export function materializeContainerTrace(marker: {
958984 // surfaces as an unhandled error in dev. The root is never disposed —
959985 // the projection settles itself when the trace ends and is collected
960986 // with the store.
961- return createRoot ( ( ) =>
987+ return coreRoot ( ( ) =>
962988 createProjection (
963989 ( draft : any ) => ( {
964990 [ Symbol . asyncIterator ] ( ) {
@@ -1168,6 +1194,29 @@ function hydrateStoreLike(coreFn: Function, fn: any, initialValue: any, options?
11681194 return hydrateStoreLikeFn ( coreFn , fn , initialValue , options , options ?. ssrSource ) ;
11691195}
11701196
1197+ // --- Hydration-aware root ---
1198+
1199+ // A hydrating root is the snapshot scope from its first child on. The scope
1200+ // used to be marked lazily, by the first hydration-aware primitive created
1201+ // under it — so a control-flow memo made before that (Show's condition, on
1202+ // the core createMemo) sat outside it, and a user-tier write during the
1203+ // hydration pass (onSettled, createEffect) cascaded through it live: the
1204+ // branch it revealed claimed fresh templates against the registry, missed,
1205+ // and rendered detached (#3504). Marking at the root makes the scope
1206+ // creation-order independent: every write during the pass is held and
1207+ // replays at release, once the claim pass is over.
1208+ function hydratedCreateRoot ( init : Function , options ?: { id ?: string ; transparent ?: boolean } ) {
1209+ return coreRoot (
1210+ sharedConfig . hydrating
1211+ ? dispose => {
1212+ markTopLevelSnapshotScope ( ) ;
1213+ return init ( dispose ) ;
1214+ }
1215+ : ( init as any ) ,
1216+ options
1217+ ) ;
1218+ }
1219+
11711220// --- Hydration-aware effect implementations ---
11721221
11731222function hydratedEffect ( coreFn : Function , compute : any , effectFn : any , options ?: any ) {
@@ -1271,6 +1320,7 @@ function lazyHydrationLookup<T>(
12711320}
12721321
12731322export function enableHydration ( ) {
1323+ _createRoot = hydratedCreateRoot ;
12741324 _createMemo = hydratedCreateMemo ;
12751325 _createSignal = hydratedCreateSignal ;
12761326 _createErrorBoundary = hydratedCreateErrorBoundary ;
@@ -1292,6 +1342,7 @@ export function enableHydration() {
12921342 // the exact CSR behavior onHydrationEnd itself had.
12931343 sharedConfig . isHydrationInProgress = isHydrationInProgress ;
12941344 sharedConfig . onHydrationEnd = onHydrationEnd ;
1345+ sharedConfig . isClaiming = isClaiming ;
12951346
12961347 // Take ownership of streamed-fragment reveals (see the fragment ledger).
12971348 // The header script creates `_$HY` before any module runs, so the hook is
@@ -1759,6 +1810,27 @@ export const createOptimisticStore: {
17591810 : ( coreOptimisticStore as Function ) ( ...args ) ;
17601811} ) as any ;
17611812
1813+ /**
1814+ * Creates a non-tracked owner scope that doesn't auto-dispose. Pass `id`
1815+ * to seed hydration ids for the tree it owns.
1816+ *
1817+ * ```ts
1818+ * const dispose = createRoot(dispose => {
1819+ * // ...
1820+ * return dispose;
1821+ * });
1822+ * ```
1823+ *
1824+ * **Hydration:** a root created during hydration marks itself as the
1825+ * snapshot scope, so writes landing during the hydration pass are held
1826+ * until the pass completes (the mark used to depend on which primitive
1827+ * ran first under it).
1828+ *
1829+ * @description https://docs.solidjs.com/reference/reactive-utilities/create-root
1830+ */
1831+ export const createRoot : typeof coreRoot = ( ( ...args : any [ ] ) =>
1832+ ( _createRoot || coreRoot ) ( ...args ) ) as typeof coreRoot ;
1833+
17621834/**
17631835 * Creates a reactive computation that runs during the render phase as
17641836 * DOM elements are created and updated but not necessarily connected.
@@ -1886,6 +1958,7 @@ function resumeBoundaryHydration(
18861958 // synchronous resume window; without a capture the live globals apply.
18871959 const prevRegistry = sharedConfig . registry ;
18881960 const prevGather = sharedConfig . gather ;
1961+ const prevClaim = _claimOwner ;
18891962 if ( scope ) {
18901963 sharedConfig . registry = scope . registry ;
18911964 sharedConfig . gather = scope . gather ;
@@ -1896,14 +1969,21 @@ function resumeBoundaryHydration(
18961969 if ( shouldHydrate ) {
18971970 markSnapshotScope ( o ) ;
18981971 _snapshotRootOwner = o ;
1972+ // The window claims this boundary's subtree only: the rest of the
1973+ // tree hydrated in the root pass, and a re-render it takes during the
1974+ // window (a write from the resumed content's user effects) is a
1975+ // client render (#3504).
1976+ _claimOwner = o ;
18991977 }
19001978 set ( ) ;
19011979 flush ( ) ;
19021980 if ( shouldHydrate ) _snapshotRootOwner = null ;
19031981 _hydratingValue = false ;
1982+ _claimOwner = prevClaim ;
19041983 if ( shouldHydrate ) releaseSnapshotScope ( o ) ;
19051984 flush ( ) ;
19061985 } finally {
1986+ _claimOwner = prevClaim ;
19071987 if ( scope ) {
19081988 sharedConfig . registry = prevRegistry ;
19091989 sharedConfig . gather = prevGather ;
0 commit comments