MCPcopy Create free account
hub / github.com/angular/angular / withDomHydration

Function withDomHydration

packages/core/src/hydration/api.ts:194–309  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

192 * configure or change anything in NgUniversal to enable the feature.
193 */
194export function withDomHydration(): EnvironmentProviders {
195 const providers: Provider[] = [
196 {
197 provide: IS_HYDRATION_DOM_REUSE_ENABLED,
198 useFactory: () => {
199 let isEnabled = true;
200 if (typeof ngServerMode === 'undefined' || !ngServerMode) {
201 // On the client, verify that the server response contains
202 // hydration annotations. Otherwise, keep hydration disabled.
203 const transferState = inject(TransferState, {optional: true});
204 isEnabled = !!transferState?.get(NGH_DATA_KEY, null);
205 }
206 if (isEnabled) {
207 performanceMarkFeature('NgHydration');
208 }
209 return isEnabled;
210 },
211 },
212 {
213 provide: ENVIRONMENT_INITIALIZER,
214 useValue: () => {
215 // i18n support is enabled by calling withI18nSupport(), but there's
216 // no way to turn it off (e.g. for tests), so we turn it off by default.
217 setIsI18nHydrationSupportEnabled(false);
218
219 if (typeof ngServerMode !== 'undefined' && ngServerMode) {
220 // Since this function is used across both server and client,
221 // make sure that the runtime code is only added when invoked
222 // on the client (see the `enableHydrationRuntimeSupport` function
223 // call below).
224 return;
225 }
226
227 const doc = inject(DOCUMENT);
228 if (inject(IS_HYDRATION_DOM_REUSE_ENABLED)) {
229 verifySsrContentsIntegrity(doc);
230 enableHydrationRuntimeSupport();
231 } else if (
232 typeof ngDevMode !== 'undefined' &&
233 ngDevMode &&
234 !inject(IS_TESTING) &&
235 !isClientRenderModeEnabled(doc)
236 ) {
237 const console = inject(Console);
238 const message = formatRuntimeError(
239 RuntimeErrorCode.MISSING_HYDRATION_ANNOTATIONS,
240 'Angular hydration was requested on the client, but there was no ' +
241 'serialized information present in the server response, ' +
242 'thus hydration was not enabled. ' +
243 'Make sure the `provideClientHydration()` is included into the list ' +
244 'of providers in the server part of the application configuration.',
245 );
246 console.warn(message);
247 }
248 },
249 multi: true,
250 },
251 ];

Callers 1

provideClientHydrationFunction · 0.85

Calls 15

injectFunction · 0.90
performanceMarkFeatureFunction · 0.90
formatRuntimeErrorFunction · 0.90
cleanupDehydratedViewsFunction · 0.90
makeEnvironmentProvidersFunction · 0.90
whenStableWithTimeoutFunction · 0.85
printHydrationStatsFunction · 0.85

Tested by

no test coverage detected