Skip to content

Commit 6a75658

Browse files
committed
refactor(@angular/ssr): remove duplicate code and streamline serve functionality
This commit cleans up duplicate code left from the previous implementations of process, serve, and render. Additionally, prerender serve now exclusively handles HEAD and GET requests, aligning with updated handling requirements. The private `renderStatic` method has been removed in favor of the `handle` method for improved maintainability.
1 parent 481ccdb commit 6a75658

File tree

11 files changed

+262
-206
lines changed

11 files changed

+262
-206
lines changed

goldens/public-api/angular/ssr/index.api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { EnvironmentProviders } from '@angular/core';
99
// @public
1010
export class AngularAppEngine {
1111
handle(request: Request, requestContext?: unknown): Promise<Response | null>;
12+
static ɵallowStaticRouteRender: boolean;
1213
static ɵhooks: Hooks;
1314
}
1415

packages/angular/build/src/tools/vite/middlewares/ssr-middleware.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export function createAngularSsrInternalMiddleware(
4747
const angularServerApp = ɵgetOrCreateAngularServerApp();
4848
// Only Add the transform hook only if it's a different instance.
4949
if (cachedAngularServerApp !== angularServerApp) {
50+
angularServerApp.allowStaticRouteRender = true;
5051
angularServerApp.hooks.on('html:transform:pre', async ({ html, url }) => {
5152
const processedHtml = await server.transformIndexHtml(url.pathname, html);
5253

@@ -96,6 +97,7 @@ export async function createAngularSsrExternalMiddleware(
9697
reqHandler?: unknown;
9798
AngularAppEngine: typeof SSRAngularAppEngine;
9899
};
100+
99101
if (!isSsrNodeRequestHandler(reqHandler) && !isSsrRequestHandler(reqHandler)) {
100102
if (!fallbackWarningShown) {
101103
// eslint-disable-next-line no-console
@@ -118,6 +120,7 @@ export async function createAngularSsrExternalMiddleware(
118120
}
119121

120122
if (cachedAngularAppEngine !== AngularAppEngine) {
123+
AngularAppEngine.ɵallowStaticRouteRender = true;
121124
AngularAppEngine.ɵhooks.on('html:transform:pre', async ({ html, url }) => {
122125
const processedHtml = await server.transformIndexHtml(url.pathname, html);
123126

packages/angular/build/src/utils/server-rendering/prerender.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ async function getAllRoutes(
279279

280280
if (appShellOptions) {
281281
routes.push({
282+
renderMode: RouteRenderMode.AppShell,
282283
route: urlJoin(baseHref, appShellOptions.route),
283284
});
284285
}
@@ -287,6 +288,7 @@ async function getAllRoutes(
287288
const routesFromFile = (await readFile(routesFile, 'utf8')).split(/\r?\n/);
288289
for (const route of routesFromFile) {
289290
routes.push({
291+
renderMode: RouteRenderMode.Prerender,
290292
route: urlJoin(baseHref, route.trim()),
291293
});
292294
}

packages/angular/build/src/utils/server-rendering/render-worker.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ async function renderPage({ url }: RenderOptions): Promise<string | null> {
4040
const { ɵgetOrCreateAngularServerApp: getOrCreateAngularServerApp } =
4141
await loadEsmModuleFromMemory('./main.server.mjs');
4242
const angularServerApp = getOrCreateAngularServerApp();
43-
const response = await angularServerApp.renderStatic(
44-
new URL(url, serverURL),
45-
AbortSignal.timeout(30_000),
43+
angularServerApp.allowStaticRouteRender = true;
44+
const response = await angularServerApp.handle(
45+
new Request(new URL(url, serverURL), { signal: AbortSignal.timeout(30_000) }),
4646
);
4747

4848
return response ? response.text() : null;

packages/angular/ssr/src/app-engine.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import type { AngularServerApp } from './app';
1010
import { Hooks } from './hooks';
1111
import { getPotentialLocaleIdFromUrl } from './i18n';
1212
import { EntryPointExports, getAngularAppEngineManifest } from './manifest';
13-
import { stripIndexHtmlFromURL, stripTrailingSlash } from './utils/url';
1413

1514
/**
1615
* Angular server application engine.
@@ -24,23 +23,23 @@ import { stripIndexHtmlFromURL, stripTrailingSlash } from './utils/url';
2423
*/
2524
export class AngularAppEngine {
2625
/**
27-
* Hooks for extending or modifying the behavior of the server application.
28-
* These hooks are used by the Angular CLI when running the development server and
29-
* provide extensibility points for the application lifecycle.
26+
* A flag to enable or disable the rendering of prerendered routes.
27+
*
28+
* Typically used during development to avoid prerendering all routes ahead of time,
29+
* allowing them to be rendered on the fly as requested.
3030
*
3131
* @private
3232
*/
33-
static ɵhooks = /* #__PURE__*/ new Hooks();
33+
static ɵallowStaticRouteRender = false;
3434

3535
/**
36-
* Provides access to the hooks for extending or modifying the server application's behavior.
37-
* This allows attaching custom functionality to various server application lifecycle events.
36+
* Hooks for extending or modifying the behavior of the server application.
37+
* These hooks are used by the Angular CLI when running the development server and
38+
* provide extensibility points for the application lifecycle.
3839
*
39-
* @internal
40+
* @private
4041
*/
41-
get hooks(): Hooks {
42-
return AngularAppEngine.ɵhooks;
43-
}
42+
static ɵhooks = /* #__PURE__*/ new Hooks();
4443

4544
/**
4645
* The manifest for the server application.
@@ -93,7 +92,8 @@ export class AngularAppEngine {
9392
// be located in separate bundles, making `instanceof` checks unreliable.
9493
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
9594
const serverApp = getOrCreateAngularServerApp() as AngularServerApp;
96-
serverApp.hooks = this.hooks;
95+
serverApp.hooks = AngularAppEngine.ɵhooks;
96+
serverApp.allowStaticRouteRender = AngularAppEngine.ɵallowStaticRouteRender;
9797

9898
return serverApp;
9999
}

0 commit comments

Comments
 (0)