Skip to content

Commit db00a24

Browse files
bjarklerfilipesilva
authored andcommitted
feat(@angular-devkit/build-angular): support custom headers in dev-server
Make it possible to configure dev-server to send custom HTTP headers on every client request. These headers can be specified as a key-value map under the new "headers" property of the dev-server builder in angular.json. These headers are then passed on to the webpack devserver. An example use case for this is to enable various security features, such as CSP and Trusted Types, both in local application development and in integration tests, by setting appropriate HTTP headers. This is part of an effort to add support for Trusted Types in Angular. The ability to enforce Trusted Types during development and integration tests is essential, as this can help detect Trusted Types violations that might otherwise break applications when they're pushed to production where such security features may be enforced.
1 parent 533ff17 commit db00a24

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

packages/angular_devkit/build_angular/src/dev-server/index.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -397,10 +397,13 @@ export function buildServerConfig(
397397
const servePath = buildServePath(serverOptions, browserOptions, logger);
398398
const { styles, scripts } = normalizeOptimization(browserOptions.optimization);
399399

400-
const config: WebpackDevServer.Configuration & { logLevel: string } = {
400+
const config: WebpackDevServer.Configuration&{logLevel: string} = {
401401
host: serverOptions.host,
402402
port: serverOptions.port,
403-
headers: { 'Access-Control-Allow-Origin': '*' },
403+
headers: {
404+
'Access-Control-Allow-Origin': '*',
405+
...serverOptions.headers,
406+
},
404407
historyApiFallback: !!browserOptions.index && {
405408
index: `${servePath}/${getIndexOutputFile(browserOptions)}`,
406409
disableDotRule: true,

packages/angular_devkit/build_angular/src/dev-server/schema.json

+10
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@
3636
"type": "string",
3737
"description": "SSL certificate to use for serving HTTPS."
3838
},
39+
"headers": {
40+
"type": "object",
41+
"description": "Custom HTTP headers to serve.",
42+
"propertyNames": {
43+
"pattern": "^[-_A-Za-z0-9]+$"
44+
},
45+
"additionalProperties": {
46+
"type": "string"
47+
}
48+
},
3949
"open": {
4050
"type": "boolean",
4151
"description": "Opens the url in default browser.",

packages/angular_devkit/build_angular/src/dev-server/works_spec.ts

+10
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,14 @@ describe('Dev Server Builder', () => {
102102
expect(hasSourceMaps).toBe(false, `Expected emitted files not to contain '.map' files.`);
103103
});
104104

105+
it('serves custom headers', async () => {
106+
const run = await architect.scheduleTarget(
107+
target, {headers: {'X-Header': 'Hello World'}});
108+
runs.push(run);
109+
const output = await run.result as DevServerBuilderOutput;
110+
expect(output.success).toBe(true);
111+
const response = await fetch('http://localhost:4200/index.html');
112+
expect(response.headers.get('X-Header')).toBe('Hello World');
113+
}, 30000);
114+
105115
});

0 commit comments

Comments
 (0)