Skip to content

Commit df1d388

Browse files
committed
fix(@angular/build): configure Vite CORS option
Vite's `allowedHosts` option does not enable CORS; instead, it allows the dev server to respond to requests with a matching hostname (e.g., http://example.com/main.js). It only verifies that the request’s hostname is on the allowed list. However, this does not consider the `origin` in the case of a CORS request. This commit updates Vite's configuration to enable CORS. Closes #29549 (cherry picked from commit be15b88)
1 parent d2e1c8e commit df1d388

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

packages/angular/build/src/builders/dev-server/schema.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@
3737
"description": "SSL certificate to use for serving HTTPS."
3838
},
3939
"allowedHosts": {
40-
"description": "The hosts that can access the development server. This option sets the Vite option of the same name. For further details: https://vite.dev/config/server-options.html#server-allowedhosts",
40+
"description": "The hosts that the development server will respond to. This option sets the Vite option of the same name. For further details: https://vite.dev/config/server-options.html#server-allowedhosts",
4141
"default": [],
4242
"oneOf": [
4343
{
4444
"type": "array",
45-
"description": "List of hosts that are allowed to access the development server.",
45+
"description": "A list of hosts that the development server will respond to.",
4646
"items": {
4747
"type": "string"
4848
}

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

+3
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,9 @@ export async function setupServer(
841841
? (proxy ?? {})
842842
: proxy,
843843
cors: {
844+
// This will add the header `Access-Control-Allow-Origin: http://example.com`,
845+
// where `http://example.com` is the requesting origin.
846+
origin: true,
844847
// Allow preflight requests to be proxied.
845848
preflightContinue: true,
846849
},

0 commit comments

Comments
 (0)