@@ -20,42 +20,44 @@ export function hmrInvalidateClientOnlyModulesInRsc(): Vite.Plugin {
2020 return ;
2121 }
2222
23- // Find the corresponding client module for this file so we can walk the
24- // module graph
25- const updatedClientModule =
26- ctx . server . environments . client . moduleGraph . getModuleById ( ctx . file ) ;
27-
28- // If this file is not in the client graph, it's not a client-only module
29- // and we don't need to invalidate anything, so bail out
30- if ( ! updatedClientModule ) {
23+ // Find the corresponding client modules for this file so we can walk the
24+ // module graph looking for ancestors in the RSC graph
25+ const updatedClientModules =
26+ ctx . server . environments . client . moduleGraph . getModulesByFile ( ctx . file ) ;
27+ if ( ! updatedClientModules ) {
3128 return ;
3229 }
3330
34- const visited = new Set < Vite . EnvironmentModuleNode > ( ) ;
35- const walk = ( module : Vite . EnvironmentModuleNode ) => {
36- if ( ! module || visited . has ( module ) || ! module . id ) {
37- return ;
38- }
39-
40- visited . add ( module ) ;
41-
42- // If this module is in the RSC graph, invalidate it and stop walking
43- const serverModule = this . environment . moduleGraph . getModuleById (
44- module . id ,
45- ) ;
46- if ( serverModule ) {
47- this . environment . moduleGraph . invalidateModule ( serverModule ) ;
48- return ;
49- }
50-
51- if ( module . importers ) {
52- for ( const importer of module . importers ) {
53- walk ( importer ) ;
31+ for ( const updatedClientModule of updatedClientModules ) {
32+ const visited = new Set < Vite . EnvironmentModuleNode > ( ) ;
33+ const walk = ( module : Vite . EnvironmentModuleNode ) => {
34+ if ( visited . has ( module ) || ! module . id ) {
35+ return ;
5436 }
55- }
56- } ;
5737
58- walk ( updatedClientModule ) ;
38+ visited . add ( module ) ;
39+
40+ // Try to find this module in the RSC graph
41+ const serverModule = this . environment . moduleGraph . getModuleById (
42+ module . id ,
43+ ) ;
44+
45+ // If this module is in the RSC graph, invalidate it and stop walking
46+ if ( serverModule ) {
47+ this . environment . moduleGraph . invalidateModule ( serverModule ) ;
48+ return ;
49+ }
50+
51+ // If we haven't found a corresponding RSC module, walk importers
52+ if ( module . importers ) {
53+ for ( const importer of module . importers ) {
54+ walk ( importer ) ;
55+ }
56+ }
57+ } ;
58+
59+ walk ( updatedClientModule ) ;
60+ }
5961 } ,
6062 } ;
6163}
0 commit comments