@@ -71,6 +71,38 @@ function removeEventDispatchScript(doc: Document) {
71
71
findEventDispatchScript ( doc ) ?. remove ( ) ;
72
72
}
73
73
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
+
74
106
/**
75
107
* Creates a marker comment node and append it into the `<body>`.
76
108
* Some CDNs have mechanisms to remove all comment node from HTML.
@@ -124,31 +156,14 @@ function insertEventRecordScript(
124
156
}
125
157
126
158
async function _render ( platformRef : PlatformRef , applicationRef : ApplicationRef ) : Promise < string > {
127
- const environmentInjector = applicationRef . injector ;
128
-
129
159
// Block until application is stable.
130
160
await whenStable ( applicationRef ) ;
131
161
132
162
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 ) ;
150
164
151
165
// Run any BEFORE_APP_SERIALIZED callbacks just before rendering to string.
166
+ const environmentInjector = applicationRef . injector ;
152
167
const callbacks = environmentInjector . get ( BEFORE_APP_SERIALIZED , null ) ;
153
168
if ( callbacks ) {
154
169
const asyncCallbacks : Promise < void > [ ] = [ ] ;
0 commit comments