Skip to content

Commit dd18bda

Browse files
thekhegayalan-agius4
authored andcommitted
fix(@schematics/angular): include the SSR entry file in the trust-proxy-headers migration
migration collected only `build.options.server`, which in application builder projects is `src/main.server.ts`, while `AngularNodeAppEngine` is created in `ssr.entry` file (`src/server.ts`) so on default SSR workspace it made no changes and after update app stoped trusting `X-Forwarded-Host` and `X-Forwarded-Proto`, now `ssr.entry` file is collected too (cherry picked from commit 43c401c)
1 parent c7852e2 commit dd18bda

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

‎packages/schematics/angular/migrations/trust-proxy-headers/migration.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9+
import { isJsonObject } from '@angular-devkit/core';
910
import type { Rule } from '@angular-devkit/schematics';
1011
import { Visitor, parseSync } from 'oxc-parser';
1112
import { allTargetOptions, allWorkspaceTargets, getWorkspace } from '../../utility/workspace';
@@ -28,6 +29,13 @@ export default function (): Rule {
2829
if (typeof options?.['server'] === 'string') {
2930
serverFiles.add(options['server']);
3031
}
32+
33+
// With the application builder, `server` is the server bootstrap file (`main.server.ts`).
34+
// The app engine is created in the `ssr.entry` file (`server.ts`).
35+
const ssr = options?.['ssr'];
36+
if (ssr && isJsonObject(ssr) && typeof ssr['entry'] === 'string') {
37+
serverFiles.add(ssr['entry']);
38+
}
3139
}
3240
}
3341

‎packages/schematics/angular/migrations/trust-proxy-headers/migration_spec.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,46 @@ describe(`Migration to add trustProxyHeaders to server.ts`, () => {
150150
expect(content).toContain(`[trustProxyHeaders]: true`);
151151
});
152152

153+
it(`should add trustProxyHeaders to the server file set in the 'ssr.entry' option`, async () => {
154+
tree.overwrite(
155+
'/angular.json',
156+
JSON.stringify({
157+
version: 1,
158+
projects: {
159+
app: {
160+
root: '',
161+
sourceRoot: 'src',
162+
projectType: 'application',
163+
architect: {
164+
build: {
165+
options: {
166+
server: 'src/main.server.ts',
167+
outputMode: 'server',
168+
ssr: { entry: 'src/server.ts' },
169+
},
170+
},
171+
},
172+
},
173+
},
174+
}),
175+
);
176+
const mainServerContent =
177+
`import { bootstrapApplication } from '@angular/platform-browser';\n` +
178+
`export default (context) => bootstrapApplication(App, config, context);`;
179+
tree.create('/src/main.server.ts', mainServerContent);
180+
tree.create(
181+
'/src/server.ts',
182+
`import { AngularNodeAppEngine } from '@angular/ssr/node';\nconst angularApp = new AngularNodeAppEngine();`,
183+
);
184+
185+
const newTree = await schematicRunner.runSchematic(schematicName, {}, tree);
186+
const content = newTree.readText('/src/server.ts');
187+
expect(content).toContain(`const angularApp = new AngularNodeAppEngine({`);
188+
expect(content).toContain(TODO_COMMENT);
189+
expect(content).toContain(`trustProxyHeaders: ['x-forwarded-host', 'x-forwarded-proto'],`);
190+
expect(newTree.readText('/src/main.server.ts')).toBe(mainServerContent);
191+
});
192+
153193
it(`should skip files with parse errors without throwing`, async () => {
154194
const malformedContent = `import { AngularAppEngine } from '@angular/ssr';\nconst angularApp = new AngularAppEngine(;`;
155195
tree.create('/server.ts', malformedContent);

0 commit comments

Comments
 (0)