Skip to content

Commit 75cf47e

Browse files
committed
fix(@angular/ssr): apply HTML transformation to CSR responses
Before this commit, HTML transformations were not applied to CSR responses, leading to the omission of the Vite client code. Closes #29033 (cherry picked from commit f897b7a)
1 parent 75b4e92 commit 75cf47e

File tree

1 file changed

+23
-16
lines changed
  • packages/angular/ssr/src

1 file changed

+23
-16
lines changed

packages/angular/ssr/src/app.ts

+23-16
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,7 @@ import { InlineCriticalCssProcessor } from './utils/inline-critical-css';
2525
import { LRUCache } from './utils/lru-cache';
2626
import { AngularBootstrap, renderAngular } from './utils/ng';
2727
import { promiseWithAbort } from './utils/promise';
28-
import {
29-
buildPathWithParams,
30-
joinUrlParts,
31-
stripIndexHtmlFromURL,
32-
stripLeadingSlash,
33-
} from './utils/url';
28+
import { buildPathWithParams, joinUrlParts, stripLeadingSlash } from './utils/url';
3429

3530
/**
3631
* Maximum number of critical CSS entries the cache can store.
@@ -256,6 +251,7 @@ export class AngularServerApp {
256251
return null;
257252
}
258253

254+
const url = new URL(request.url);
259255
const platformProviders: StaticProvider[] = [];
260256

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

291290
const {
@@ -301,16 +300,9 @@ export class AngularServerApp {
301300
});
302301
}
303302

304-
const url = new URL(request.url);
305-
let html = await assets.getIndexServerHtml().text();
306-
307-
// Skip extra microtask if there are no pre hooks.
308-
if (hooks.has('html:transform:pre')) {
309-
html = await hooks.run('html:transform:pre', { html, url });
310-
}
311-
312303
this.boostrap ??= await bootstrap();
313-
304+
let html = await assets.getIndexServerHtml().text();
305+
html = await this.runTransformsOnHtml(html, url);
314306
html = await renderAngular(
315307
html,
316308
this.boostrap,
@@ -381,6 +373,21 @@ export class AngularServerApp {
381373

382374
return stripLeadingSlash(assetPath);
383375
}
376+
377+
/**
378+
* Runs the registered transform hooks on the given HTML content.
379+
*
380+
* @param html - The raw HTML content to be transformed.
381+
* @param url - The URL associated with the HTML content, used for context during transformations.
382+
* @returns A promise that resolves to the transformed HTML string.
383+
*/
384+
private async runTransformsOnHtml(html: string, url: URL): Promise<string> {
385+
if (this.hooks.has('html:transform:pre')) {
386+
html = await this.hooks.run('html:transform:pre', { html, url });
387+
}
388+
389+
return html;
390+
}
384391
}
385392

386393
let angularServerApp: AngularServerApp | undefined;

0 commit comments

Comments
 (0)