Skip to content

Commit ad014c7

Browse files
committed
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.
1 parent f05b053 commit ad014c7

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
lines changed

packages/angular/ssr/src/app.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -215,15 +215,15 @@ export class AngularServerApp {
215215
let html = await assets.getIndexServerHtml();
216216
// Skip extra microtask if there are no pre hooks.
217217
if (hooks.has('html:transform:pre')) {
218-
html = await hooks.run('html:transform:pre', { html });
218+
html = await hooks.run('html:transform:pre', { html, url });
219219
}
220220

221221
this.boostrap ??= await bootstrap();
222222

223223
html = await renderAngular(
224224
html,
225225
this.boostrap,
226-
new URL(request.url),
226+
url,
227227
platformProviders,
228228
SERVER_CONTEXT_VALUE[renderMode],
229229
);

packages/angular/ssr/src/hooks.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
*/
88

99
/**
10-
* Handler function type for HTML transformation hooks.
11-
* It takes an object containing the HTML content to be modified.
10+
* Defines a handler function type for transforming HTML content.
11+
* This function receives an object with the HTML to be processed.
1212
*
13-
* @param ctx - The context object containing the HTML content.
14-
* @returns The modified HTML content or a promise that resolves to the modified HTML content.
13+
* @param ctx - An object containing the URL and HTML content to be transformed.
14+
* @returns The transformed HTML as a string or a promise that resolves to the transformed HTML.
1515
*/
16-
type HtmlTransformHandler = (ctx: { html: string }) => string | Promise<string>;
16+
type HtmlTransformHandler = (ctx: { url: URL; html: string }) => string | Promise<string>;
1717

1818
/**
1919
* Defines the names of available hooks for registering and triggering custom logic within the application.

packages/angular/ssr/test/hooks_spec.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { Hooks } from '../src/hooks';
1010

1111
describe('Hooks', () => {
1212
let hooks: Hooks & { run: Function };
13+
const url = new URL('http://example.com/');
1314

1415
beforeEach(() => {
1516
hooks = new Hooks() as Hooks & { run: Function };
@@ -33,12 +34,12 @@ describe('Hooks', () => {
3334
hooks.on('html:transform:pre', ({ html }) => html + '1');
3435
hooks.on('html:transform:pre', ({ html }) => html + '2');
3536

36-
const result = await hooks.run('html:transform:pre', { html: 'start' });
37+
const result = await hooks.run('html:transform:pre', { html: 'start', url });
3738
expect(result).toBe('start12');
3839
});
3940

4041
it('should return the context html if no hooks are registered', async () => {
41-
const result = await hooks.run('html:transform:pre', { html: 'start' });
42+
const result = await hooks.run('html:transform:pre', { html: 'start', url });
4243
expect(result).toBe('start');
4344
});
4445

0 commit comments

Comments
 (0)