Skip to content

Commit 741cca7

Browse files
committed
feat(@schematics/angular): add ng new --ssr
This commit enabled users to opt-in adding SSR and SSG to their application during the `ng new` experience. This can be done either by using the `--ssr` option or answer `Yes` when prompted.
1 parent 6a85b13 commit 741cca7

File tree

8 files changed

+37
-5
lines changed

8 files changed

+37
-5
lines changed

Diff for: docs/design/analytics.md

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ PROJECT NAME TO BUILD OR A MODULE NAME.**
5858
| SchematicCollectionName | `ep.ng_schematic_collection_name` | `string` |
5959
| SchematicName | `ep.ng_schematic_name` | `string` |
6060
| Standalone | `ep.ng_standalone` | `string` |
61+
| SSR | `ep.ng_ssr` | `string` |
6162
| Style | `ep.ng_style` | `string` |
6263
| Routing | `ep.ng_routing` | `string` |
6364
| InlineTemplate | `ep.ng_inline_template` | `string` |

Diff for: packages/angular/cli/src/analytics/analytics-parameters.ts

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export enum EventCustomDimension {
7272
SchematicCollectionName = 'ep.ng_schematic_collection_name',
7373
SchematicName = 'ep.ng_schematic_name',
7474
Standalone = 'ep.ng_standalone',
75+
SSR = 'ep.ng_ssr',
7576
Style = 'ep.ng_style',
7677
Routing = 'ep.ng_routing',
7778
InlineTemplate = 'ep.ng_inline_template',

Diff for: packages/angular/ssr/schematics/ng-add/index.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import { Rule, externalSchematic } from '@angular-devkit/schematics';
9+
import { externalSchematic } from '@angular-devkit/schematics';
1010
import { Schema as SSROptions } from './schema';
1111

12-
export default function (options: SSROptions): Rule {
13-
return externalSchematic('@schematics/angular', 'ssr', options);
14-
}
12+
export default (options: SSROptions) => externalSchematic('@schematics/angular', 'ssr', options);

Diff for: packages/angular/ssr/schematics/ng-add/index_spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/te
1111

1212
import { join } from 'node:path';
1313

14-
describe('SSR Schematic', () => {
14+
describe('@angular/ssr ng-add schematic', () => {
1515
const defaultOptions = {
1616
project: 'test-app',
1717
};

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

+5
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ export default function (options: ApplicationOptions): Rule {
9898
]),
9999
MergeStrategy.Overwrite,
100100
),
101+
options.ssr
102+
? schematic('ssr', {
103+
project: options.name,
104+
})
105+
: noop(),
101106
options.skipPackageJson ? noop() : addDependenciesToPackageJson(options),
102107
]);
103108
};

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

+14
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,20 @@ describe('Application Schematic', () => {
169169
});
170170
});
171171

172+
it(`should create an application with SSR features when 'ssr=true'`, async () => {
173+
const options = { ...defaultOptions, ssr: true };
174+
const filePath = '/projects/foo/server.ts';
175+
expect(workspaceTree.exists(filePath)).toBeFalse();
176+
const tree = await schematicRunner.runSchematic('application', options, workspaceTree);
177+
expect(tree.exists(filePath)).toBeTrue();
178+
});
179+
180+
it(`should not create an application with SSR features when 'ssr=false'`, async () => {
181+
const options = { ...defaultOptions, ssr: false };
182+
const tree = await schematicRunner.runSchematic('application', options, workspaceTree);
183+
expect(tree.exists('/projects/foo/server.ts')).toBeFalse();
184+
});
185+
172186
describe(`update package.json`, () => {
173187
it(`should add build-angular to devDependencies`, async () => {
174188
const tree = await schematicRunner.runSchematic('application', defaultOptions, workspaceTree);

Diff for: packages/schematics/angular/application/schema.json

+7
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@
107107
"type": "boolean",
108108
"default": true,
109109
"x-user-analytics": "ep.ng_standalone"
110+
},
111+
"ssr": {
112+
"description": "Creates an application with Server-Side Rendering (SSR) and Static Site Generation (SSG/Prerendering) enabled.",
113+
"x-prompt": "Do you want to enable Server-Side Rendering (SSR) and Static Site Generation (SSG/Prerendering)?",
114+
"type": "boolean",
115+
"default": false,
116+
"x-user-analytics": "ep.ng_ssr"
110117
}
111118
},
112119
"required": ["name"]

Diff for: packages/schematics/angular/ng-new/schema.json

+6
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@
139139
"type": "boolean",
140140
"default": true,
141141
"x-user-analytics": "ep.ng_standalone"
142+
},
143+
"ssr": {
144+
"description": "Creates an application with Server-Side Rendering (SSR) and Static Site Generation (SSG/Prerendering) enabled.",
145+
"type": "boolean",
146+
"default": false,
147+
"x-user-analytics": "ep.ng_ssr"
142148
}
143149
},
144150
"required": ["name", "version"]

0 commit comments

Comments
 (0)