@@ -73,22 +73,30 @@ export const addStyle = (styleContainerNode: any, cmpMeta: d.ComponentRuntimeMet
7373 let appliedStyles = rootAppliedStyles . get ( styleContainerNode ) ;
7474 let styleElm ;
7575 if ( ! appliedStyles ) {
76- rootAppliedStyles . set ( styleContainerNode , ( appliedStyles = new Set ( ) ) ) ;
76+ rootAppliedStyles . set ( styleContainerNode , ( appliedStyles = new Map ( ) ) ) ;
7777 }
7878
79- // Check if style element already exists (for HMR updates)
80- // For shadow DOM components, directly update their dedicated style element
81- // For scoped components, check if they have their own HMR-created style element
82- const existingStyleElm : HTMLStyleElement =
83- ( BUILD . hydrateClientSide || BUILD . hotModuleReplacement ) &&
84- styleContainerNode . querySelector ( `[${ HYDRATED_STYLE_ID } ="${ scopeId } "]` ) ;
79+ // Check if tracked element is still in the DOM (fixes #6637)
80+ const trackedElm = appliedStyles . get ( scopeId ) ;
81+ if ( trackedElm !== undefined ) {
82+ if ( trackedElm === null || trackedElm . parentNode === styleContainerNode ) {
83+ return scopeId ;
84+ }
85+ appliedStyles . delete ( scopeId ) ;
86+ }
87+
88+ const existingStyleElm : HTMLStyleElement | undefined =
89+ ( ( BUILD . hydrateClientSide || BUILD . hotModuleReplacement ) &&
90+ styleContainerNode . querySelector ( `[${ HYDRATED_STYLE_ID } ="${ scopeId } "]` ) ) ||
91+ undefined ;
8592
8693 if ( existingStyleElm ) {
87- // Update existing style element (for hydration or HMR)
8894 existingStyleElm . textContent = style ;
89- } else if ( ! appliedStyles . has ( scopeId ) ) {
95+ appliedStyles . set ( scopeId , existingStyleElm ) ;
96+ } else {
9097 styleElm = win . document . createElement ( 'style' ) ;
9198 styleElm . textContent = style ;
99+ let appliedStyleElm = styleElm ;
92100
93101 // Apply CSP nonce to the style tag if it exists
94102 const nonce = plt . $nonce$ ?? queryNonceMetaTagContent ( win . document ) ;
@@ -165,6 +173,7 @@ export const addStyle = (styleContainerNode: any, cmpMeta: d.ComponentRuntimeMet
165173 const existingStyleContainer : HTMLStyleElement = styleContainerNode . querySelector ( 'style' ) ;
166174 if ( existingStyleContainer && ! BUILD . hotModuleReplacement ) {
167175 existingStyleContainer . textContent = style + existingStyleContainer . textContent ;
176+ appliedStyleElm = existingStyleContainer ;
168177 } else {
169178 ( styleContainerNode as HTMLElement ) . prepend ( styleElm ) ;
170179 }
@@ -186,14 +195,12 @@ export const addStyle = (styleContainerNode: any, cmpMeta: d.ComponentRuntimeMet
186195 styleElm . textContent += SLOT_FB_CSS ;
187196 }
188197
189- if ( appliedStyles ) {
190- appliedStyles . add ( scopeId ) ;
191- }
198+ appliedStyles . set ( scopeId , appliedStyleElm ) ;
192199 }
193200 } else if ( BUILD . constructableCSS ) {
194201 let appliedStyles = rootAppliedStyles . get ( styleContainerNode ) ;
195202 if ( ! appliedStyles ) {
196- rootAppliedStyles . set ( styleContainerNode , ( appliedStyles = new Set ( ) ) ) ;
203+ rootAppliedStyles . set ( styleContainerNode , ( appliedStyles = new Map ( ) ) ) ;
197204 }
198205 if ( ! appliedStyles . has ( scopeId ) ) {
199206 /**
@@ -220,7 +227,7 @@ export const addStyle = (styleContainerNode: any, cmpMeta: d.ComponentRuntimeMet
220227 styleContainerNode . adoptedStyleSheets = [ ...styleContainerNode . adoptedStyleSheets , stylesheet ] ;
221228 }
222229
223- appliedStyles . add ( scopeId ) ;
230+ appliedStyles . set ( scopeId , null ) ;
224231
225232 // Remove SSR style element from shadow root now that adoptedStyleSheets is in use
226233 // Only remove from shadow roots, not from document head (for scoped components)
0 commit comments