Skip to content

Commit 33cd47c

Browse files
committed
fix(@angular/build): properly configure headers for media resources and HTML page
Headers were not configured correctly. Closes #27464
1 parent 908150d commit 33cd47c

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import { executeDevServer } from '../../index';
10+
import { executeOnceAndFetch } from '../execute-fetch';
11+
import { describeServeBuilder } from '../jasmine-helpers';
12+
import { BASE_OPTIONS, DEV_SERVER_BUILDER_INFO } from '../setup';
13+
14+
describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupTarget) => {
15+
describe('option: "headers"', () => {
16+
beforeEach(async () => {
17+
setupTarget(harness, {
18+
styles: ['src/styles.css'],
19+
});
20+
21+
// Application code is not needed for these tests
22+
await harness.writeFile('src/main.ts', '');
23+
await harness.writeFile('src/styles.css', '');
24+
});
25+
26+
it('index response headers should include configured header', async () => {
27+
harness.useTarget('serve', {
28+
...BASE_OPTIONS,
29+
headers: {
30+
'x-custom': 'foo',
31+
},
32+
});
33+
34+
const { result, response } = await executeOnceAndFetch(harness, '/');
35+
36+
expect(result?.success).toBeTrue();
37+
expect(await response?.headers.get('x-custom')).toBe('foo');
38+
});
39+
40+
it('media resource response headers should include configured header', async () => {
41+
await harness.writeFiles({
42+
'src/styles.css': `h1 { background: url('./test.svg')}`,
43+
'src/test.svg': `<svg xmlns="http://www.w3.org/2000/svg">
44+
<text x="20" y="20" font-size="20" fill="red">Hello World</text>
45+
</svg>`,
46+
});
47+
48+
harness.useTarget('serve', {
49+
...BASE_OPTIONS,
50+
headers: {
51+
'x-custom': 'foo',
52+
},
53+
});
54+
55+
const { result, response } = await executeOnceAndFetch(harness, '/media/test.svg');
56+
57+
expect(result?.success).toBeTrue();
58+
expect(await response?.headers.get('x-custom')).toBe('foo');
59+
});
60+
});
61+
});

packages/angular/build/src/builders/dev-server/vite-server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,7 @@ export async function setupServer(
556556
outputFiles,
557557
assets,
558558
ssr,
559+
extraHeaders: serverOptions.headers,
559560
external: externalMetadata.explicit,
560561
indexHtmlTransformer,
561562
extensionMiddleware,

0 commit comments

Comments
 (0)