@@ -71,6 +71,38 @@ function removeEventDispatchScript(doc: Document) {
7171 findEventDispatchScript ( doc ) ?. remove ( ) ;
7272}
7373
74+ /**
75+ * Annotate nodes for hydration and remove event dispatch script when not needed.
76+ */
77+ function prepareForHydration ( platformState : PlatformState , applicationRef : ApplicationRef ) : void {
78+ const environmentInjector = applicationRef . injector ;
79+ const doc = platformState . getDocument ( ) ;
80+
81+ if ( ! environmentInjector . get ( IS_HYDRATION_DOM_REUSE_ENABLED , false ) ) {
82+ // Hydration is diabled, remove inlined event dispatch script.
83+ // (which was injected by the build process) from the HTML.
84+ removeEventDispatchScript ( doc ) ;
85+
86+ return ;
87+ }
88+
89+ appendSsrContentIntegrityMarker ( doc ) ;
90+
91+ const eventTypesToBeReplayed = annotateForHydration ( applicationRef , doc ) ;
92+ if ( eventTypesToBeReplayed ) {
93+ insertEventRecordScript (
94+ environmentInjector . get ( APP_ID ) ,
95+ doc ,
96+ eventTypesToBeReplayed ,
97+ environmentInjector . get ( CSP_NONCE , null ) ,
98+ ) ;
99+ } else {
100+ // No events to replay, we should remove inlined event dispatch script
101+ // (which was injected by the build process) from the HTML.
102+ removeEventDispatchScript ( doc ) ;
103+ }
104+ }
105+
74106/**
75107 * Creates a marker comment node and append it into the `<body>`.
76108 * Some CDNs have mechanisms to remove all comment node from HTML.
@@ -124,31 +156,14 @@ function insertEventRecordScript(
124156}
125157
126158async function _render ( platformRef : PlatformRef , applicationRef : ApplicationRef ) : Promise < string > {
127- const environmentInjector = applicationRef . injector ;
128-
129159 // Block until application is stable.
130160 await whenStable ( applicationRef ) ;
131161
132162 const platformState = platformRef . injector . get ( PlatformState ) ;
133- if ( environmentInjector . get ( IS_HYDRATION_DOM_REUSE_ENABLED , false ) ) {
134- const doc = platformState . getDocument ( ) ;
135- appendSsrContentIntegrityMarker ( doc ) ;
136- const eventTypesToBeReplayed = annotateForHydration ( applicationRef , doc ) ;
137- if ( eventTypesToBeReplayed ) {
138- insertEventRecordScript (
139- environmentInjector . get ( APP_ID ) ,
140- doc ,
141- eventTypesToBeReplayed ,
142- environmentInjector . get ( CSP_NONCE , null ) ,
143- ) ;
144- } else {
145- // No events to replay, we should remove inlined event dispatch script
146- // (which was injected by the build process) from the HTML.
147- removeEventDispatchScript ( doc ) ;
148- }
149- }
163+ prepareForHydration ( platformState , applicationRef ) ;
150164
151165 // Run any BEFORE_APP_SERIALIZED callbacks just before rendering to string.
166+ const environmentInjector = applicationRef . injector ;
152167 const callbacks = environmentInjector . get ( BEFORE_APP_SERIALIZED , null ) ;
153168 if ( callbacks ) {
154169 const asyncCallbacks : Promise < void > [ ] = [ ] ;
0 commit comments