66 * found in the LICENSE file at https://angular.io/license
77 */
88
9+ import { ɵwithHttpTransferCache as withHttpTransferCache } from '@angular/common/http' ;
910import { EnvironmentProviders , makeEnvironmentProviders , Provider , ɵwithDomHydration as withDomHydration } from '@angular/core' ;
1011
1112/**
12- * The list of features as an enum to uniquely type each feature.
13+ * The list of features as an enum to uniquely type each `HydrationFeature`.
14+ * @see HydrationFeature
15+ *
16+ * @publicApi
17+ * @developerPreview
1318 */
1419export const enum HydrationFeatureKind {
15- NoDomReuseFeature
20+ NoDomReuseFeature ,
21+ NoHttpTransferCache
1622}
1723
1824/**
@@ -30,49 +36,36 @@ export interface HydrationFeature<FeatureKind extends HydrationFeatureKind> {
3036 * Helper function to create an object that represents a Hydration feature.
3137 */
3238function hydrationFeature < FeatureKind extends HydrationFeatureKind > (
33- kind : FeatureKind , providers : Provider [ ] ) : HydrationFeature < FeatureKind > {
39+ kind : FeatureKind , providers : Provider [ ] = [ ] ) : HydrationFeature < FeatureKind > {
3440 return { ɵkind : kind , ɵproviders : providers } ;
3541}
3642
37- /**
38- * A type alias that represents a feature which disables DOM reuse during hydration
39- * (effectively making Angular re-render the whole application from scratch).
40- * The type is used to describe the return value of the `withoutDomReuse` function.
41- *
42- * @see `withoutDomReuse`
43- * @see `provideClientHydration`
44- *
45- * @publicApi
46- * @developerPreview
47- */
48- export type NoDomReuseFeature = HydrationFeature < HydrationFeatureKind . NoDomReuseFeature > ;
49-
5043/**
5144 * Disables DOM nodes reuse during hydration. Effectively makes
5245 * Angular re-render an application from scratch on the client.
5346 *
5447 * @publicApi
5548 * @developerPreview
5649 */
57- export function withoutDomReuse ( ) : NoDomReuseFeature {
50+ export function withNoDomReuse ( ) : HydrationFeature < HydrationFeatureKind . NoDomReuseFeature > {
5851 // This feature has no providers and acts as a flag that turns off
5952 // non-destructive hydration (which otherwise is turned on by default).
60- const providers : Provider [ ] = [ ] ;
61- return hydrationFeature ( HydrationFeatureKind . NoDomReuseFeature , providers ) ;
53+ return hydrationFeature ( HydrationFeatureKind . NoDomReuseFeature ) ;
6254}
6355
6456/**
65- * A type alias that represents all Hydration features available for use with
66- * `provideClientHydration`. Features can be enabled by adding special functions to the
67- * `provideClientHydration` call. See documentation for each symbol to find corresponding
68- * function name. See also `provideClientHydration` documentation on how to use those functions.
69- *
70- * @see `provideClientHydration`
57+ * Disables HTTP transfer cache. Effectively causes HTTP requests to be performed twice: once on the
58+ * server and other one on the browser.
7159 *
7260 * @publicApi
7361 * @developerPreview
7462 */
75- export type HydrationFeatures = NoDomReuseFeature ;
63+ export function withNoHttpTransferCache ( ) :
64+ HydrationFeature < HydrationFeatureKind . NoHttpTransferCache > {
65+ // This feature has no providers and acts as a flag that turns off
66+ // HTTP transfer cache (which otherwise is turned on by default).
67+ return hydrationFeature ( HydrationFeatureKind . NoHttpTransferCache ) ;
68+ }
7669
7770/**
7871 * Sets up providers necessary to enable hydration functionality for the application.
@@ -102,19 +95,31 @@ export type HydrationFeatures = NoDomReuseFeature;
10295 * export class AppModule {}
10396 * ```
10497 *
105- * @see `HydrationFeatures`
98+ * @see `withNoDomReuse`
99+ * @see `withNoHttpTransferCache`
106100 *
107101 * @param features Optional features to configure additional router behaviors.
108102 * @returns A set of providers to enable hydration.
109103 *
110104 * @publicApi
111105 * @developerPreview
112106 */
113- export function provideClientHydration ( ...features : HydrationFeatures [ ] ) : EnvironmentProviders {
114- const shouldUseDomHydration =
115- ! features . find ( feature => feature . ɵkind === HydrationFeatureKind . NoDomReuseFeature ) ;
107+ export function provideClientHydration ( ...features : HydrationFeature < HydrationFeatureKind > [ ] ) :
108+ EnvironmentProviders {
109+ const providers : Provider [ ] = [ ] ;
110+ const featuresKind = new Set < HydrationFeatureKind > ( ) ;
111+
112+ for ( const { ɵproviders, ɵkind} of features ) {
113+ featuresKind . add ( ɵkind ) ;
114+
115+ if ( ɵproviders . length ) {
116+ providers . push ( ɵproviders ) ;
117+ }
118+ }
119+
116120 return makeEnvironmentProviders ( [
117- ( shouldUseDomHydration ? withDomHydration ( ) : [ ] ) ,
118- features . map ( feature => feature . ɵproviders ) ,
121+ ( featuresKind . has ( HydrationFeatureKind . NoDomReuseFeature ) ? [ ] : withDomHydration ( ) ) ,
122+ ( featuresKind . has ( HydrationFeatureKind . NoHttpTransferCache ) ? [ ] : withHttpTransferCache ( ) ) ,
123+ providers ,
119124 ] ) ;
120125}
0 commit comments