Skip to content

Commit

Permalink
refactor(@angular/build): implement custom middleware for header appe…
Browse files Browse the repository at this point in the history
…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
alan-agius4 committed Sep 13, 2024
1 parent 62b2c6c commit 740c648
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type { Connect, Plugin } from 'vite';
import {
angularHtmlFallbackMiddleware,
createAngularAssetsMiddleware,
createAngularHeadersMiddleware,
createAngularIndexHtmlMiddleware,
createAngularSSRMiddleware,
} from './middlewares';
Expand Down Expand Up @@ -114,6 +115,8 @@ export function createAngularMemoryPlugin(options: AngularMemoryPluginOptions):
};
};

server.middlewares.use(createAngularHeadersMiddleware(server));

// Assets and resources get handled first
server.middlewares.use(
createAngularAssetsMiddleware(server, assets, outputFiles, usedComponentStyles),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ import { lookup as lookupMimeType } from 'mrmime';
import { extname } from 'node:path';
import type { Connect, ViteDevServer } from 'vite';
import { loadEsmModule } from '../../../utils/load-esm';
import {
AngularMemoryOutputFiles,
appendServerConfiguredHeaders,
pathnameWithoutBasePath,
} from '../utils';
import { AngularMemoryOutputFiles, pathnameWithoutBasePath } from '../utils';

const COMPONENT_REGEX = /%COMP%/g;

Expand Down Expand Up @@ -97,7 +93,6 @@ export function createAngularAssetsMiddleware(

res.setHeader('Content-Type', 'text/css');
res.setHeader('Cache-Control', 'no-cache');
appendServerConfiguredHeaders(server, res);
res.end(encapsulatedData);
})
.catch((e) => next(e));
Expand All @@ -116,7 +111,6 @@ export function createAngularAssetsMiddleware(
res.setHeader('Content-Type', mimeType);
}
res.setHeader('Cache-Control', 'no-cache');
appendServerConfiguredHeaders(server, res);
res.end(data);

return;
Expand Down
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();
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@

import { extname } from 'node:path';
import type { Connect, ViteDevServer } from 'vite';
import {
AngularMemoryOutputFiles,
appendServerConfiguredHeaders,
pathnameWithoutBasePath,
} from '../utils';
import { AngularMemoryOutputFiles, pathnameWithoutBasePath } from '../utils';

export function createAngularIndexHtmlMiddleware(
server: ViteDevServer,
Expand Down Expand Up @@ -52,7 +48,6 @@ export function createAngularIndexHtmlMiddleware(

res.setHeader('Content-Type', 'text/html');
res.setHeader('Cache-Control', 'no-cache');
appendServerConfiguredHeaders(server, res);
res.end(processedHtml);
})
.catch((error) => next(error));
Expand Down
1 change: 1 addition & 0 deletions packages/angular/build/src/tools/vite/middlewares/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export { createAngularAssetsMiddleware } from './assets-middleware';
export { angularHtmlFallbackMiddleware } from './html-fallback-middleware';
export { createAngularIndexHtmlMiddleware } from './index-html-middleware';
export { createAngularSSRMiddleware } from './ssr-middleware';
export { createAngularHeadersMiddleware } from './headers-middleware';
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import type { ɵgetOrCreateAngularServerApp as getOrCreateAngularServerApp } from '@angular/ssr';
import type { ServerResponse } from 'node:http';
import type { Connect, ViteDevServer } from 'vite';
import { appendServerConfiguredHeaders } from '../utils';

export function createAngularSSRMiddleware(
server: ViteDevServer,
Expand Down Expand Up @@ -55,7 +54,6 @@ export function createAngularSSRMiddleware(
return next();
}

appendServerConfiguredHeaders(server, res);
res.end(content);
})
.catch((error) => next(error));
Expand Down
18 changes: 0 additions & 18 deletions packages/angular/build/src/tools/vite/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
*/

import { lookup as lookupMimeType } from 'mrmime';
import type { IncomingMessage, ServerResponse } from 'node:http';
import { extname } from 'node:path';
import type { ViteDevServer } from 'vite';

export type AngularMemoryOutputFiles = Map<string, { contents: Uint8Array; servable: boolean }>;

Expand All @@ -32,19 +30,3 @@ export function lookupMimeTypeFromRequest(url: string): string | undefined {

return extension && lookupMimeType(extension);
}

export function appendServerConfiguredHeaders(
server: ViteDevServer,
res: ServerResponse<IncomingMessage>,
): void {
const headers = server.config.server.headers;
if (!headers) {
return;
}

for (const [name, value] of Object.entries(headers)) {
if (value !== undefined) {
res.setHeader(name, value);
}
}
}

0 comments on commit 740c648

Please sign in to comment.