Skip to content

Commit

Permalink
fix(@angular/build): configure Vite CORS option
Browse files Browse the repository at this point in the history
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 angular#29549
  • Loading branch information
alan-agius4 committed Feb 11, 2025
1 parent 833dc98 commit 5d3cc6b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/angular/build/src/builders/dev-server/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@
"description": "SSL certificate to use for serving HTTPS."
},
"allowedHosts": {
"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",
"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",
"default": [],
"oneOf": [
{
"type": "array",
"description": "List of hosts that are allowed to access the development server.",
"description": "A list of hosts that the development server will respond to.",
"items": {
"type": "string"
}
Expand Down
2 changes: 2 additions & 0 deletions packages/angular/build/src/builders/dev-server/vite-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,8 @@ export async function setupServer(
? (proxy ?? {})
: proxy,
cors: {
// This will include `Access-Control-Allow-Origin: http://{{Origin}}`
origin: true,
// Allow preflight requests to be proxied.
preflightContinue: true,
},
Expand Down

0 comments on commit 5d3cc6b

Please sign in to comment.