Skip to content

Commit 6979eba

Browse files
committed
feat(@schematics/angular): enable hydration when adding SSR, SSG or AppShell
This commits updates the internal server schematic that is used by SSR and AppShell schematics to include `provideClientHydration` in the list of providers, which causes hydration to be enabled for application. For more information about `provideClientHydration`, see: https://angular.io/api/platform-browser/provideClientHydration
1 parent 6daf23f commit 6979eba

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

Diff for: packages/schematics/angular/server/index.ts

+6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
} from '@angular-devkit/schematics';
2323
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
2424
import { posix } from 'node:path';
25+
import { addRootProvider } from '../utility';
2526
import {
2627
NodeDependencyType,
2728
addPackageJsonDependency,
@@ -220,6 +221,11 @@ export default function (options: ServerOptions): Rule {
220221
updateConfigFileBrowserBuilder(options, tsConfigDirectory),
221222
]),
222223
addDependencies(),
224+
addRootProvider(
225+
options.project,
226+
({ code, external }) =>
227+
code`${external('provideClientHydration', '@angular/platform-browser')}()`,
228+
),
223229
]);
224230
};
225231
}

Diff for: packages/schematics/angular/server/index_spec.ts

+12
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ describe('Server Schematic', () => {
9191
expect(contents.compilerOptions.types).toEqual(['node']);
9292
expect(contents.files).toEqual(['src/main.ts', 'src/main.server.ts']);
9393
});
94+
95+
it(`should add 'provideClientHydration' to the providers list`, async () => {
96+
const tree = await schematicRunner.runSchematic('server', defaultOptions, appTree);
97+
const contents = tree.readContent('/projects/bar/src/app/app.module.ts');
98+
expect(contents).toContain(`provideClientHydration()`);
99+
});
94100
});
95101

96102
describe('standalone application', () => {
@@ -128,6 +134,12 @@ describe('Server Schematic', () => {
128134
const contents = tree.readContent(filePath);
129135
expect(contents).toContain(`const serverConfig: ApplicationConfig = {`);
130136
});
137+
138+
it(`should add 'provideClientHydration' to the providers list`, async () => {
139+
const tree = await schematicRunner.runSchematic('server', defaultOptions, appTree);
140+
const contents = tree.readContent('/projects/bar/src/app/app.config.ts');
141+
expect(contents).toContain(`providers: [provideClientHydration()]`);
142+
});
131143
});
132144

133145
describe('Legacy browser builder', () => {

0 commit comments

Comments
 (0)