Skip to content

Commit 573ec2b

Browse files
committed
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. BREAKING CHANGE: The development server (ng serve) no longer automatically mirrors the request origin in the Access-Control-Allow-Origin response header by default. If your application relies on cross-origin requests during local development, you must now explicitly configure the required CORS headers using the headers option in your angular.json configuration. Fixes #32923
1 parent 94aa77c commit 573ec2b

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,20 @@ 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+
4054
it('media resource response headers should include configured header', async () => {
4155
await harness.writeFiles({
4256
'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)