-
Notifications
You must be signed in to change notification settings - Fork 25.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(platform-browser): add a public API function to enable non-destr…
…uctive hydration (#49666) This commit adds the `provideClientHydration` function to the public API. This function can be used to enable the non-destructive Angular hydration. Important note: the non-destructive hydration feature is in Developer Preview mode, learn more about it at https://angular.io/guide/releases#developer-preview. Before you can get started with hydration, you must have a server side rendered (SSR) application. Follow the [Angular Universal Guide](https://angular.io/guide/universal) to enable server side rendering first. Once you have SSR working with your application, you can enable hydration by visiting your main app component or module and importing `provideClientHydration` from `@angular/platform-browser`. You'll then add that provider to your app's bootstrapping providers list. ```typescript import { bootstrapApplication, provideClientHydration, } from '@angular/platform-browser'; // ... bootstrapApplication(RootCmp, { providers: [provideClientHydration()] }); ``` Alternatively if you are using NgModules, you would add `provideClientHydration` to your root app module's provider list. ```typescript import {provideClientHydration} from '@angular/platform-browser'; import {NgModule} from '@angular/core'; @NgModule({ declarations: [RootCmp], exports: [RootCmp], bootstrap: [RootCmp], providers: [provideClientHydration()], }) export class AppModule {} ``` You can confirm hydration is enabled by opening Developer Tools in your browser and viewing the console. You should see a message that includes hydration-related stats, such as the number of components and nodes hydrated. Co-authored-by: jessicajaniuk <[email protected]> Co-authored-by: alan-agius4 <[email protected]> PR Close #49666
- Loading branch information
1 parent
b2327f4
commit 761e02d
Showing
6 changed files
with
224 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {EnvironmentProviders, makeEnvironmentProviders, Provider, ɵwithDomHydration as withDomHydration} from '@angular/core'; | ||
|
||
/** | ||
* The list of features as an enum to uniquely type each feature. | ||
*/ | ||
export const enum HydrationFeatureKind { | ||
NoDomReuseFeature | ||
} | ||
|
||
/** | ||
* Helper type to represent a Hydration feature. | ||
* | ||
* @publicApi | ||
* @developerPreview | ||
*/ | ||
export interface HydrationFeature<FeatureKind extends HydrationFeatureKind> { | ||
ɵkind: FeatureKind; | ||
ɵproviders: Provider[]; | ||
} | ||
|
||
/** | ||
* Helper function to create an object that represents a Hydration feature. | ||
*/ | ||
function hydrationFeature<FeatureKind extends HydrationFeatureKind>( | ||
kind: FeatureKind, providers: Provider[]): HydrationFeature<FeatureKind> { | ||
return {ɵkind: kind, ɵproviders: providers}; | ||
} | ||
|
||
/** | ||
* A type alias that represents a feature which disables DOM reuse during hydration | ||
* (effectively making Angular re-render the whole application from scratch). | ||
* The type is used to describe the return value of the `withoutDomReuse` function. | ||
* | ||
* @see `withoutDomReuse` | ||
* @see `provideClientHydration` | ||
* | ||
* @publicApi | ||
* @developerPreview | ||
*/ | ||
export type NoDomReuseFeature = HydrationFeature<HydrationFeatureKind.NoDomReuseFeature>; | ||
|
||
/** | ||
* Disables DOM nodes reuse during hydration. Effectively makes | ||
* Angular re-render an application from scratch on the client. | ||
* | ||
* @publicApi | ||
* @developerPreview | ||
*/ | ||
export function withoutDomReuse(): NoDomReuseFeature { | ||
// This feature has no providers and acts as a flag that turns off | ||
// non-destructive hydration (which otherwise is turned on by default). | ||
const providers: Provider[] = []; | ||
return hydrationFeature(HydrationFeatureKind.NoDomReuseFeature, providers); | ||
} | ||
|
||
/** | ||
* A type alias that represents all Hydration features available for use with | ||
* `provideClientHydration`. Features can be enabled by adding special functions to the | ||
* `provideClientHydration` call. See documentation for each symbol to find corresponding | ||
* function name. See also `provideClientHydration` documentation on how to use those functions. | ||
* | ||
* @see `provideClientHydration` | ||
* | ||
* @publicApi | ||
* @developerPreview | ||
*/ | ||
export type HydrationFeatures = NoDomReuseFeature; | ||
|
||
/** | ||
* Sets up providers necessary to enable hydration functionality for the application. | ||
* By default, the function enables the recommended set of features for the optimal | ||
* performance for most of the applications. You can enable/disable features by | ||
* passing special functions (from the `HydrationFeatures` set) as arguments to the | ||
* `provideClientHydration` function. | ||
* | ||
* @usageNotes | ||
* | ||
* Basic example of how you can enable hydration in your application when | ||
* `bootstrapApplication` function is used: | ||
* ``` | ||
* bootstrapApplication(AppComponent, { | ||
* providers: [provideClientHydration()] | ||
* }); | ||
* ``` | ||
* | ||
* Alternatively if you are using NgModules, you would add `provideClientHydration` | ||
* to your root app module's provider list. | ||
* ``` | ||
* @NgModule({ | ||
* declarations: [RootCmp], | ||
* bootstrap: [RootCmp], | ||
* providers: [provideClientHydration()], | ||
* }) | ||
* export class AppModule {} | ||
* ``` | ||
* | ||
* @see `HydrationFeatures` | ||
* | ||
* @param features Optional features to configure additional router behaviors. | ||
* @returns A set of providers to enable hydration. | ||
* | ||
* @publicApi | ||
* @developerPreview | ||
*/ | ||
export function provideClientHydration(...features: HydrationFeatures[]): EnvironmentProviders { | ||
const shouldUseDomHydration = | ||
!features.find(feature => feature.ɵkind === HydrationFeatureKind.NoDomReuseFeature); | ||
return makeEnvironmentProviders([ | ||
(shouldUseDomHydration ? withDomHydration() : []), | ||
features.map(feature => feature.ɵproviders), | ||
]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters