Skip to content

Commit bf5d995

Browse files
alan-agius4pkozlowski-opensource
authored andcommitted
refactor(platform-server): expose internal render method for extensibility (angular#60416)
The `renderApplication` and `renderModule` methods currently encapsulate the entire rendering process, making it difficult to intercept key phases from a non-Angular context. This change exports the internal `render` method, allowing us to perform operations such as: - Flushing headers before hydration preparation - Handling non static redirects (e.g., 302 responses) - Intercepting router events for additional processing This refactor serves as an experimental step toward improving the API for better customization and integration in the future. PR Close angular#60416
1 parent 4930ed3 commit bf5d995

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

packages/platform-server/src/private_export.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ export {
1010
INTERNAL_SERVER_PLATFORM_PROVIDERS as ɵINTERNAL_SERVER_PLATFORM_PROVIDERS,
1111
SERVER_RENDER_PROVIDERS as ɵSERVER_RENDER_PROVIDERS,
1212
} from './server';
13-
export {SERVER_CONTEXT as ɵSERVER_CONTEXT} from './utils';
13+
export {SERVER_CONTEXT as ɵSERVER_CONTEXT, renderInternal as ɵrenderInternal} from './utils';
1414
export {ENABLE_DOM_EMULATION as ɵENABLE_DOM_EMULATION} from './tokens';

packages/platform-server/src/utils.ts

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -174,17 +174,22 @@ function insertEventRecordScript(
174174
stopMeasuring(measuringLabel);
175175
}
176176

177-
async function _render(platformRef: PlatformRef, applicationRef: ApplicationRef): Promise<string> {
178-
const measuringLabel = 'whenStable';
179-
startMeasuring(measuringLabel);
180-
181-
// Block until application is stable.
182-
await applicationRef.whenStable();
183-
184-
stopMeasuring(measuringLabel);
185-
177+
/**
178+
* Renders an Angular application to a string.
179+
*
180+
* @private
181+
*
182+
* @param platformRef - Reference to the Angular platform.
183+
* @param applicationRef - Reference to the Angular application.
184+
* @returns A promise that resolves to the rendered string.
185+
*/
186+
export async function renderInternal(
187+
platformRef: PlatformRef,
188+
applicationRef: ApplicationRef,
189+
): Promise<string> {
186190
const platformState = platformRef.injector.get(PlatformState);
187191
prepareForHydration(platformState, applicationRef);
192+
appendServerContextInfo(applicationRef);
188193

189194
// Run any BEFORE_APP_SERIALIZED callbacks just before rendering to string.
190195
const environmentInjector = applicationRef.injector;
@@ -212,8 +217,6 @@ async function _render(platformRef: PlatformRef, applicationRef: ApplicationRef)
212217
}
213218
}
214219

215-
appendServerContextInfo(applicationRef);
216-
217220
return platformState.renderToString();
218221
}
219222

@@ -273,7 +276,14 @@ export async function renderModule<T>(
273276
try {
274277
const moduleRef = await platformRef.bootstrapModule(moduleType);
275278
const applicationRef = moduleRef.injector.get(ApplicationRef);
276-
return await _render(platformRef, applicationRef);
279+
280+
const measuringLabel = 'whenStable';
281+
startMeasuring(measuringLabel);
282+
// Block until application is stable.
283+
await applicationRef.whenStable();
284+
stopMeasuring(measuringLabel);
285+
286+
return await renderInternal(platformRef, applicationRef);
277287
} finally {
278288
await asyncDestroyPlatform(platformRef);
279289
}
@@ -315,7 +325,14 @@ export async function renderApplication<T>(
315325
stopMeasuring(bootstrapLabel);
316326

317327
startMeasuring(_renderLabel);
318-
const rendered = await _render(platformRef, applicationRef);
328+
329+
const measuringLabel = 'whenStable';
330+
startMeasuring(measuringLabel);
331+
// Block until application is stable.
332+
await applicationRef.whenStable();
333+
stopMeasuring(measuringLabel);
334+
335+
const rendered = await renderInternal(platformRef, applicationRef);
319336
stopMeasuring(_renderLabel);
320337
return rendered;
321338
} finally {

0 commit comments

Comments
 (0)