Skip to content

Commit c33e862

Browse files
committed
refactor(@angular/build): Enable SSR with Prerendering Disabled in Vite
This commit enables server-side rendering (SSR) in Vite when prerendering is turned off. It also imports `@angular/compiler` in the SSR middleware to resolve the following issue: ``` [vite] Internal server error: The injectable 'PlatformNavigation' needs to be compiled using the JIT compiler, but '@angular/compiler' is not available. The injectable is part of a library that has been partially compiled. However, the Angular Linker has not processed the library to utilize JIT compilation as a fallback. Ideally, the library should be processed with the Angular Linker for complete AOT compilation. ``` Closes #28523
1 parent 41934e7 commit c33e862

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

packages/angular/build/src/builders/dev-server/vite-server.ts

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ export async function* serveWithVite(
103103
// Disable prerendering if enabled and force SSR.
104104
// This is so instead of prerendering all the routes for every change, the page is "prerendered" when it is requested.
105105
browserOptions.prerender = false;
106+
browserOptions.ssr ||= true;
106107
}
107108

108109
// Set all packages as external to support Vite's prebundle caching

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

+7
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ export function createAngularSsrInternalMiddleware(
3434
}
3535

3636
(async () => {
37+
// Load the compiler because `@angular/ssr/node` depends on `@angular/` packages,
38+
// which must be processed by the runtime linker, even if they are not used.
39+
await loadEsmModule('@angular/compiler');
3740
const { writeResponseToNodeResponse, createWebRequestFromNodeRequest } =
3841
await loadEsmModule<typeof import('@angular/ssr/node')>('@angular/ssr/node');
3942

@@ -76,6 +79,10 @@ export async function createAngularSsrExternalMiddleware(
7679
| ReturnType<typeof createAngularSsrInternalMiddleware>
7780
| undefined;
7881

82+
// Load the compiler because `@angular/ssr/node` depends on `@angular/` packages,
83+
// which must be processed by the runtime linker, even if they are not used.
84+
await loadEsmModule('@angular/compiler');
85+
7986
const { createWebRequestFromNodeRequest, writeResponseToNodeResponse } =
8087
await loadEsmModule<typeof import('@angular/ssr/node')>('@angular/ssr/node');
8188

0 commit comments

Comments
 (0)