66 * found in the LICENSE file at https://angular.dev/license
77 */
88
9- import type { Injector } from '../di' ;
109import { DehydratedBlockRegistry } from '../defer/registry' ;
1110import { processAndInitTriggers } from '../defer/triggering' ;
11+ import type { Injector } from '../di' ;
12+ import { InjectionToken } from '../di/injection_token' ;
13+ import { INJECTOR } from '../render3/interfaces/view' ;
14+ import { getLView } from '../render3/state' ;
1215import { performanceMarkFeature } from '../util/performance' ;
1316import { gatherDeferBlocksCommentNodes } from './node_lookup_utils' ;
1417import {
@@ -34,6 +37,18 @@ let _runIncrementalHydrationBootstrap: (injector: Injector, doc: Document) => vo
3437 */
3538let isIncrementalHydrationRuntimeActive = false ;
3639
40+ interface IncrementalHydrationBootstrapState {
41+ requested : boolean ;
42+ activated : boolean ;
43+ injector : Injector ;
44+ document : Document ;
45+ }
46+
47+ export const INCREMENTAL_HYDRATION_BOOTSTRAP =
48+ new InjectionToken < IncrementalHydrationBootstrapState > (
49+ typeof ngDevMode === 'undefined' || ngDevMode ? 'INCREMENTAL_HYDRATION_BOOTSTRAP' : '' ,
50+ ) ;
51+
3752/**
3853 * Returns the `DehydratedBlockRegistry` used by incremental hydration, or
3954 * `null` if the runtime has not been activated.
@@ -43,39 +58,60 @@ export function createDehydratedBlockRegistry(): DehydratedBlockRegistry | null
4358}
4459
4560/**
46- * Runs the incremental-hydration bootstrap routine (defer-block scanning,
47- * trigger initialization, jsaction wiring). Called from `APP_BOOTSTRAP_LISTENER`.
48- * No-op until the runtime is activated.
61+ * Runs incremental hydration bootstrap once application bootstrap and the
62+ * compiler-generated runtime activation have both occurred.
4963 */
50- export function runIncrementalHydrationBootstrap ( injector : Injector , doc : Document ) : void {
51- _runIncrementalHydrationBootstrap ( injector , doc ) ;
64+ export function runIncrementalHydrationBootstrap ( state : IncrementalHydrationBootstrapState ) : void {
65+ if ( state . requested && state . activated ) {
66+ _runIncrementalHydrationBootstrap ( state . injector , state . document ) ;
67+ }
5268}
5369
5470/**
5571 * Activates the incremental-hydration runtime.
5672 *
57- * Emitted by the Angular compiler at the top level of every component file
58- * whose template contains a `@defer (hydrate ...)` trigger. The first call
59- * swaps in real implementations; subsequent calls are no-ops.
73+ * Emitted by the Angular compiler before the first hydrating `@defer` block in
74+ * a creation block. The first call swaps in the real implementations. Every
75+ * client call also notifies the current application that the runtime is
76+ * available.
6077 *
6178 * @codeGenApi
6279 */
6380export function ɵɵenableIncrementalHydrationRuntime ( ) : void {
64- if ( isIncrementalHydrationRuntimeActive ) {
65- return ;
66- }
67- isIncrementalHydrationRuntimeActive = true ;
81+ if ( ! isIncrementalHydrationRuntimeActive ) {
82+ isIncrementalHydrationRuntimeActive = true ;
6883
69- enableRetrieveDeferBlockDataImpl ( ) ;
84+ enableRetrieveDeferBlockDataImpl ( ) ;
7085
71- performanceMarkFeature ( 'NgIncrementalHydration' ) ;
86+ performanceMarkFeature ( 'NgIncrementalHydration' ) ;
7287
73- _dehydratedBlockRegistryFactory = ( ) => new DehydratedBlockRegistry ( ) ;
88+ _dehydratedBlockRegistryFactory = ( ) => new DehydratedBlockRegistry ( ) ;
7489
75- _runIncrementalHydrationBootstrap = ( injector , doc ) => {
76- const deferBlockData = processBlockData ( injector ) ;
77- const commentsByBlockId = gatherDeferBlocksCommentNodes ( doc , doc . body ) ;
78- processAndInitTriggers ( injector , deferBlockData , commentsByBlockId ) ;
79- appendDeferBlocksToJSActionMap ( doc , injector ) ;
80- } ;
90+ _runIncrementalHydrationBootstrap = ( injector , doc ) => {
91+ const deferBlockData = processBlockData ( injector ) ;
92+ const commentsByBlockId = gatherDeferBlocksCommentNodes ( doc , doc . body ) ;
93+ processAndInitTriggers ( injector , deferBlockData , commentsByBlockId ) ;
94+ appendDeferBlocksToJSActionMap ( doc , injector ) ;
95+ } ;
96+ }
97+
98+ if ( typeof ngServerMode === 'undefined' || ! ngServerMode ) {
99+ const injector = getLView ( ) [ INJECTOR ] ;
100+ const state = injector . get ( INCREMENTAL_HYDRATION_BOOTSTRAP , null , { optional : true } ) ;
101+ if ( state !== null && ! state . activated ) {
102+ state . activated = true ;
103+ runIncrementalHydrationBootstrap ( state ) ;
104+ }
105+ }
106+ }
107+
108+ /**
109+ * Resets module-level runtime activation for tests that run SSR and client hydration in the same
110+ * process. This is not a full hydration reset and should only be used when cold client activation
111+ * is part of the behavior under test.
112+ */
113+ export function resetIncrementalHydrationRuntimeForTests ( ) : void {
114+ isIncrementalHydrationRuntimeActive = false ;
115+ _dehydratedBlockRegistryFactory = ( ) => null ;
116+ _runIncrementalHydrationBootstrap = ( ) => { } ;
81117}
0 commit comments