refactor(@angular/ssr): update HtmlTransformHandler type to include URL parameter

Modified the `HtmlTransformHandler` type to accept a context object containing a URL and HTML content. This change supports the `html:transform:pre` hook, enhancing the handler's capability to process transformations based on the request URL.
This commit is contained in:
Alan Agius 2024-09-20 14:05:57 +00:00 committed by Alan Agius
parent f05b0537f5
commit ad014c7d9b
3 changed files with 10 additions and 9 deletions

View File

@ -215,7 +215,7 @@ export class AngularServerApp {
let html = await assets.getIndexServerHtml();
// Skip extra microtask if there are no pre hooks.
if (hooks.has('html:transform:pre')) {
html = await hooks.run('html:transform:pre', { html });
html = await hooks.run('html:transform:pre', { html, url });
}
this.boostrap ??= await bootstrap();
@ -223,7 +223,7 @@ export class AngularServerApp {
html = await renderAngular(
html,
this.boostrap,
new URL(request.url),
url,
platformProviders,
SERVER_CONTEXT_VALUE[renderMode],
);

View File

@ -7,13 +7,13 @@
*/
/**
* Handler function type for HTML transformation hooks.
* It takes an object containing the HTML content to be modified.
* Defines a handler function type for transforming HTML content.
* This function receives an object with the HTML to be processed.
*
* @param ctx - The context object containing the HTML content.
* @returns The modified HTML content or a promise that resolves to the modified HTML content.
* @param ctx - An object containing the URL and HTML content to be transformed.
* @returns The transformed HTML as a string or a promise that resolves to the transformed HTML.
*/
type HtmlTransformHandler = (ctx: { html: string }) => string | Promise<string>;
type HtmlTransformHandler = (ctx: { url: URL; html: string }) => string | Promise<string>;
/**
* Defines the names of available hooks for registering and triggering custom logic within the application.

View File

@ -10,6 +10,7 @@ import { Hooks } from '../src/hooks';
describe('Hooks', () => {
let hooks: Hooks & { run: Function };
const url = new URL('http://example.com/');
beforeEach(() => {
hooks = new Hooks() as Hooks & { run: Function };
@ -33,12 +34,12 @@ describe('Hooks', () => {
hooks.on('html:transform:pre', ({ html }) => html + '1');
hooks.on('html:transform:pre', ({ html }) => html + '2');
const result = await hooks.run('html:transform:pre', { html: 'start' });
const result = await hooks.run('html:transform:pre', { html: 'start', url });
expect(result).toBe('start12');
});
it('should return the context html if no hooks are registered', async () => {
const result = await hooks.run('html:transform:pre', { html: 'start' });
const result = await hooks.run('html:transform:pre', { html: 'start', url });
expect(result).toBe('start');
});