Skip to content

fix(@angular/ssr): apply HTML transformation to CSR responses #29038

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions packages/angular/ssr/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,7 @@ import { InlineCriticalCssProcessor } from './utils/inline-critical-css';
import { LRUCache } from './utils/lru-cache';
import { AngularBootstrap, renderAngular } from './utils/ng';
import { promiseWithAbort } from './utils/promise';
import {
buildPathWithParams,
joinUrlParts,
stripIndexHtmlFromURL,
stripLeadingSlash,
} from './utils/url';
import { buildPathWithParams, joinUrlParts, stripLeadingSlash } from './utils/url';

/**
* Maximum number of critical CSS entries the cache can store.
Expand Down Expand Up @@ -256,6 +251,7 @@ export class AngularServerApp {
return null;
}

const url = new URL(request.url);
const platformProviders: StaticProvider[] = [];

// Initialize the response with status and headers if available.
Expand Down Expand Up @@ -285,7 +281,10 @@ export class AngularServerApp {
);
} else if (renderMode === RenderMode.Client) {
// Serve the client-side rendered version if the route is configured for CSR.
return new Response(await this.assets.getServerAsset('index.csr.html').text(), responseInit);
let html = await this.assets.getServerAsset('index.csr.html').text();
html = await this.runTransformsOnHtml(html, url);

return new Response(html, responseInit);
}

const {
Expand All @@ -301,16 +300,9 @@ export class AngularServerApp {
});
}

const url = new URL(request.url);
let html = await assets.getIndexServerHtml().text();

// Skip extra microtask if there are no pre hooks.
if (hooks.has('html:transform:pre')) {
html = await hooks.run('html:transform:pre', { html, url });
}

this.boostrap ??= await bootstrap();

let html = await assets.getIndexServerHtml().text();
html = await this.runTransformsOnHtml(html, url);
html = await renderAngular(
html,
this.boostrap,
Expand Down Expand Up @@ -381,6 +373,21 @@ export class AngularServerApp {

return stripLeadingSlash(assetPath);
}

/**
* Runs the registered transform hooks on the given HTML content.
*
* @param html - The raw HTML content to be transformed.
* @param url - The URL associated with the HTML content, used for context during transformations.
* @returns A promise that resolves to the transformed HTML string.
*/
private async runTransformsOnHtml(html: string, url: URL): Promise<string> {
if (this.hooks.has('html:transform:pre')) {
html = await this.hooks.run('html:transform:pre', { html, url });
}

return html;
}
}

let angularServerApp: AngularServerApp | undefined;
Expand Down
Loading