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

Function withDomHydration

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

Source from the content-addressed store, hash-verified

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

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