Skip to content

Commit b85ec67

Browse files
authored
fix(@angular/build): allow configuring Access-Control-Allow-Origin via headers option
Removes the default Vite CORS origin: true configuration, allowing custom Access-Control-Allow-Origin header configurations to take effect when using the development server.
1 parent cd2ad3c commit b85ec67

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

packages/angular/build/src/builders/dev-server/tests/options/headers_spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,31 @@ describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupT
3737
expect(await response?.headers.get('x-custom')).toBe('foo');
3838
});
3939

40+
it('should include configured Access-Control-Allow-Origin header', async () => {
41+
harness.useTarget('serve', {
42+
...BASE_OPTIONS,
43+
headers: {
44+
'Access-Control-Allow-Origin': 'http://example.com',
45+
},
46+
});
47+
48+
const { result, response } = await executeOnceAndFetch(harness, '/main.js');
49+
50+
expect(result?.success).toBeTrue();
51+
expect(await response?.headers.get('access-control-allow-origin')).toBe('http://example.com');
52+
});
53+
54+
it('should not include Access-Control-Allow-Origin header by default', async () => {
55+
harness.useTarget('serve', {
56+
...BASE_OPTIONS,
57+
});
58+
59+
const { result, response } = await executeOnceAndFetch(harness, '/main.js');
60+
61+
expect(result?.success).toBeTrue();
62+
expect(await response?.headers.has('access-control-allow-origin')).toBeFalse();
63+
});
64+
4065
it('media resource response headers should include configured header', async () => {
4166
await harness.writeFiles({
4267
'src/styles.css': `h1 { background: url('./test.svg')}`,

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,6 @@ async function createServerConfig(
6262
ws: serverOptions.liveReload === false && serverOptions.hmr === false ? false : undefined,
6363
proxy,
6464
cors: {
65-
// This will add the header `Access-Control-Allow-Origin: http://example.com`,
66-
// where `http://example.com` is the requesting origin.
67-
origin: true,
6865
// Allow preflight requests to be proxied.
6966
preflightContinue: true,
7067
},

0 commit comments

Comments
 (0)