Skip to content

Commit

Permalink
refactor(@angular/ssr): update HtmlTransformHandler type to include…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
alan-agius4 committed Sep 23, 2024
1 parent f05b053 commit ad014c7
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
4 changes: 2 additions & 2 deletions packages/angular/ssr/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,15 @@ 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();

html = await renderAngular(
html,
this.boostrap,
new URL(request.url),
url,
platformProviders,
SERVER_CONTEXT_VALUE[renderMode],
);
Expand Down
10 changes: 5 additions & 5 deletions packages/angular/ssr/src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions packages/angular/ssr/test/hooks_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand All @@ -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');
});

Expand Down

0 comments on commit ad014c7

Please sign in to comment.