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
This commit is contained in:
Alan Agius 2025-02-12 09:42:28 +00:00 committed by Alan Agius
parent b50b6ee920
commit be15b886c7
2 changed files with 5 additions and 2 deletions

View File

@ -37,12 +37,12 @@
"description": "SSL certificate to use for serving HTTPS." "description": "SSL certificate to use for serving HTTPS."
}, },
"allowedHosts": { "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": [], "default": [],
"oneOf": [ "oneOf": [
{ {
"type": "array", "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": { "items": {
"type": "string" "type": "string"
} }

View File

@ -841,6 +841,9 @@ export async function setupServer(
? (proxy ?? {}) ? (proxy ?? {})
: proxy, : proxy,
cors: { cors: {
// This will add the header `Access-Control-Allow-Origin: http://example.com`,
// where `http://example.com` is the requesting origin.
origin: true,
// Allow preflight requests to be proxied. // Allow preflight requests to be proxied.
preflightContinue: true, preflightContinue: true,
}, },