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
This commit is contained in:
Alan Agius 2024-09-30 08:47:30 +00:00 committed by Alan Agius
parent 41934e7aa1
commit c33e862328
2 changed files with 8 additions and 0 deletions

View File

@ -103,6 +103,7 @@ export async function* serveWithVite(
// Disable prerendering if enabled and force SSR.
// This is so instead of prerendering all the routes for every change, the page is "prerendered" when it is requested.
browserOptions.prerender = false;
browserOptions.ssr ||= true;
}
// Set all packages as external to support Vite's prebundle caching

View File

@ -34,6 +34,9 @@ export function createAngularSsrInternalMiddleware(
}
(async () => {
// Load the compiler because `@angular/ssr/node` depends on `@angular/` packages,
// which must be processed by the runtime linker, even if they are not used.
await loadEsmModule('@angular/compiler');
const { writeResponseToNodeResponse, createWebRequestFromNodeRequest } =
await loadEsmModule<typeof import('@angular/ssr/node')>('@angular/ssr/node');
@ -76,6 +79,10 @@ export async function createAngularSsrExternalMiddleware(
| ReturnType<typeof createAngularSsrInternalMiddleware>
| undefined;
// Load the compiler because `@angular/ssr/node` depends on `@angular/` packages,
// which must be processed by the runtime linker, even if they are not used.
await loadEsmModule('@angular/compiler');
const { createWebRequestFromNodeRequest, writeResponseToNodeResponse } =
await loadEsmModule<typeof import('@angular/ssr/node')>('@angular/ssr/node');