@@ -17,6 +17,11 @@ import type { Connect, DepOptimizationConfig, InlineConfig, ViteDevServer } from
1717import { createAngularMemoryPlugin } from '../../tools/vite/angular-memory-plugin' ;
1818import { createAngularLocaleDataPlugin } from '../../tools/vite/i18n-locale-plugin' ;
1919import { createRemoveIdPrefixPlugin } from '../../tools/vite/id-prefix-plugin' ;
20+ import {
21+ ServerSsrMode ,
22+ createAngularSetupMiddlewaresPlugin ,
23+ } from '../../tools/vite/setup-middlewares-plugin' ;
24+ import { createAngularSsrServerPlugin } from '../../tools/vite/ssr-server-plugin' ;
2025import { loadProxyConfiguration , normalizeSourceMaps } from '../../utils' ;
2126import { loadEsmModule } from '../../utils/load-esm' ;
2227import { Result , ResultFile , ResultKind } from '../application/results' ;
@@ -313,14 +318,25 @@ export async function* serveWithVite(
313318 ? browserOptions . polyfills
314319 : [ browserOptions . polyfills ] ;
315320
321+ let ssrMode : ServerSsrMode = ServerSsrMode . NoSsr ;
322+ if (
323+ browserOptions . outputMode &&
324+ typeof browserOptions . ssr === 'object' &&
325+ browserOptions . ssr . entry
326+ ) {
327+ ssrMode = ServerSsrMode . ExternalSsrMiddleware ;
328+ } else if ( browserOptions . server ) {
329+ ssrMode = ServerSsrMode . InternalSsrMiddleware ;
330+ }
331+
316332 // Setup server and start listening
317333 const serverConfiguration = await setupServer (
318334 serverOptions ,
319335 generatedFiles ,
320336 assetFiles ,
321337 browserOptions . preserveSymlinks ,
322338 externalMetadata ,
323- ! ! browserOptions . ssr ,
339+ ssrMode ,
324340 prebundleTransformer ,
325341 target ,
326342 isZonelessApp ( polyfills ) ,
@@ -334,12 +350,6 @@ export async function* serveWithVite(
334350 server = await createServer ( serverConfiguration ) ;
335351 await server . listen ( ) ;
336352
337- if ( browserOptions . ssr && serverOptions . prebundle !== false ) {
338- // Warm up the SSR request and begin optimizing dependencies.
339- // Without this, Vite will only start optimizing SSR modules when the first request is made.
340- void server . warmupRequest ( './main.server.mjs' , { ssr : true } ) ;
341- }
342-
343353 const urls = server . resolvedUrls ;
344354 if ( urls && ( urls . local . length || urls . network . length ) ) {
345355 serverUrl = new URL ( urls . local [ 0 ] ?? urls . network [ 0 ] ) ;
@@ -385,34 +395,37 @@ async function handleUpdate(
385395 usedComponentStyles : Map < string , string [ ] > ,
386396) : Promise < void > {
387397 const updatedFiles : string [ ] = [ ] ;
388- let isServerFileUpdated = false ;
398+ let destroyAngularServerAppCalled = false ;
389399
390400 // Invalidate any updated files
391- for ( const [ file , record ] of generatedFiles ) {
392- if ( record . updated ) {
393- updatedFiles . push ( file ) ;
394- isServerFileUpdated ||= record . type === BuildOutputFileType . ServerApplication ;
401+ for ( const [ file , { updated , type } ] of generatedFiles ) {
402+ if ( ! updated ) {
403+ continue ;
404+ }
395405
396- const updatedModules = server . moduleGraph . getModulesByFile (
397- normalizePath ( join ( server . config . root , file ) ) ,
398- ) ;
399- updatedModules ?. forEach ( ( m ) => server ?. moduleGraph . invalidateModule ( m ) ) ;
406+ if ( type === BuildOutputFileType . ServerApplication && ! destroyAngularServerAppCalled ) {
407+ // Clear the server app cache
408+ // This must be done before module invalidation.
409+ const { ɵdestroyAngularServerApp } = ( await server . ssrLoadModule ( '/main.server.mjs' ) ) as {
410+ ɵdestroyAngularServerApp : typeof destroyAngularServerApp ;
411+ } ;
412+
413+ ɵdestroyAngularServerApp ( ) ;
414+ destroyAngularServerAppCalled = true ;
400415 }
416+
417+ updatedFiles . push ( file ) ;
418+
419+ const updatedModules = server . moduleGraph . getModulesByFile (
420+ normalizePath ( join ( server . config . root , file ) ) ,
421+ ) ;
422+ updatedModules ?. forEach ( ( m ) => server . moduleGraph . invalidateModule ( m ) ) ;
401423 }
402424
403425 if ( ! updatedFiles . length ) {
404426 return ;
405427 }
406428
407- // clean server apps cache
408- if ( isServerFileUpdated ) {
409- const { ɵdestroyAngularServerApp } = ( await server . ssrLoadModule ( '/main.server.mjs' ) ) as {
410- ɵdestroyAngularServerApp : typeof destroyAngularServerApp ;
411- } ;
412-
413- ɵdestroyAngularServerApp ( ) ;
414- }
415-
416429 if ( serverOptions . liveReload || serverOptions . hmr ) {
417430 if ( updatedFiles . every ( ( f ) => f . endsWith ( '.css' ) ) ) {
418431 const timestamp = Date . now ( ) ;
@@ -534,7 +547,7 @@ export async function setupServer(
534547 assets : Map < string , string > ,
535548 preserveSymlinks : boolean | undefined ,
536549 externalMetadata : DevServerExternalResultMetadata ,
537- ssr : boolean ,
550+ ssrMode : ServerSsrMode ,
538551 prebundleTransformer : JavaScriptTransformer ,
539552 target : string [ ] ,
540553 zoneless : boolean ,
@@ -587,6 +600,9 @@ export async function setupServer(
587600 preserveSymlinks,
588601 } ,
589602 server : {
603+ warmup : {
604+ ssrFiles : [ './main.server.mjs' , './server.mjs' ] ,
605+ } ,
590606 port : serverOptions . port ,
591607 strictPort : true ,
592608 host : serverOptions . host ,
@@ -637,19 +653,21 @@ export async function setupServer(
637653 } ,
638654 plugins : [
639655 createAngularLocaleDataPlugin ( ) ,
640- createAngularMemoryPlugin ( {
641- workspaceRoot : serverOptions . workspaceRoot ,
642- virtualProjectRoot,
656+ createAngularSetupMiddlewaresPlugin ( {
643657 outputFiles,
644658 assets,
645- ssr,
646- external : externalMetadata . explicitBrowser ,
647659 indexHtmlTransformer,
648660 extensionMiddleware,
649- normalizePath,
650661 usedComponentStyles,
662+ ssrMode,
651663 } ) ,
652664 createRemoveIdPrefixPlugin ( externalMetadata . explicitBrowser ) ,
665+ await createAngularSsrServerPlugin ( serverOptions . workspaceRoot ) ,
666+ await createAngularMemoryPlugin ( {
667+ virtualProjectRoot,
668+ outputFiles,
669+ external : externalMetadata . explicitBrowser ,
670+ } ) ,
653671 ] ,
654672 // Browser only optimizeDeps. (This does not run for SSR dependencies).
655673 optimizeDeps : getDepOptimizationConfig ( {
0 commit comments