-
Notifications
You must be signed in to change notification settings - Fork 12k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(@angular/build): implement custom middleware for header appe…
…nding Replaced multiple `appendServerConfiguredHeaders` calls with a single custom middleware to append headers to all responses, simplifying the code and ensuring consistency.
- Loading branch information
1 parent
62b2c6c
commit 740c648
Showing
7 changed files
with
43 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
packages/angular/build/src/tools/vite/middlewares/headers-middleware.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.dev/license | ||
*/ | ||
|
||
import type { ServerResponse } from 'node:http'; | ||
import type { Connect, ViteDevServer } from 'vite'; | ||
|
||
/** | ||
* Creates a middleware for adding custom headers. | ||
* | ||
* This middleware is responsible for setting HTTP headers as configured in the Vite server options. | ||
* If headers are defined in the server configuration, they are applied to the server response. | ||
* | ||
* @param server - The instance of `ViteDevServer` containing the configuration, including custom headers. | ||
* @returns A middleware function that processes the incoming request, sets headers if available, | ||
* and passes control to the next middleware in the chain. | ||
*/ | ||
export function createAngularHeadersMiddleware(server: ViteDevServer): Connect.NextHandleFunction { | ||
return function (_req: Connect.IncomingMessage, res: ServerResponse, next: Connect.NextFunction) { | ||
const headers = server.config.server.headers; | ||
if (!headers) { | ||
return next(); | ||
} | ||
|
||
for (const [name, value] of Object.entries(headers)) { | ||
if (value !== undefined) { | ||
res.setHeader(name, value); | ||
} | ||
} | ||
|
||
next(); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters