@@ -225,6 +225,15 @@ export async function* serveWithVite(
225225 } ) ;
226226 }
227227
228+ if ( browserOptions . ssr && server ) {
229+ // Clean up all Angular cached component IDs on every change.
230+ // This is the same thing that happens on the client (browser refresh).
231+ const { ɵresetCompiledComponents } = ( await server . ssrLoadModule ( '/main.server.mjs' ) ) as {
232+ ɵresetCompiledComponents : ( ) => void ;
233+ } ;
234+ ɵresetCompiledComponents ( ) ;
235+ }
236+
228237 let needClientUpdate = true ;
229238 switch ( result . kind ) {
230239 case ResultKind . Full :
@@ -501,24 +510,13 @@ async function invalidateUpdatedFiles(
501510 }
502511
503512 // Invalidate any updated files
504- let destroyAngularServerAppCalled = false ;
513+ let callDestroyAngularServerApp = false ;
505514 for ( const [ file , record ] of generatedFiles ) {
506515 if ( ! record . updated ) {
507516 continue ;
508517 }
509518 record . updated = false ;
510-
511- if ( record . type === BuildOutputFileType . ServerApplication && ! destroyAngularServerAppCalled ) {
512- // Clear the server app cache
513- // This must be done before module invalidation.
514- const { ɵdestroyAngularServerApp } = ( await server . ssrLoadModule ( '/main.server.mjs' ) ) as {
515- ɵdestroyAngularServerApp : typeof destroyAngularServerApp ;
516- } ;
517-
518- ɵdestroyAngularServerApp ( ) ;
519- destroyAngularServerAppCalled = true ;
520- }
521-
519+ callDestroyAngularServerApp ||= record . type === BuildOutputFileType . ServerApplication ;
522520 updatedFiles . push ( file ) ;
523521
524522 const updatedModules = server . moduleGraph . getModulesByFile (
@@ -527,9 +525,14 @@ async function invalidateUpdatedFiles(
527525 updatedModules ?. forEach ( ( m ) => server . moduleGraph . invalidateModule ( m ) ) ;
528526 }
529527
530- if ( destroyAngularServerAppCalled ) {
531- // Trigger module evaluation before reload to initiate dependency optimization.
532- await server . ssrLoadModule ( '/main.server.mjs' ) ;
528+ // Trigger module evaluation before reload to initiate dependency optimization.
529+ // and clear Angular Component IDs
530+ if ( callDestroyAngularServerApp ) {
531+ const { ɵdestroyAngularServerApp } = ( await server . ssrLoadModule ( '/main.server.mjs' ) ) as {
532+ ɵdestroyAngularServerApp : typeof destroyAngularServerApp ;
533+ } ;
534+
535+ ɵdestroyAngularServerApp ( ) ;
533536 }
534537
535538 return updatedFiles ;
0 commit comments