@@ -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 : HTMLStyleElement | null = styleElm ;
92100
93101 // Apply CSP nonce to the style tag if it exists
94102 const nonce = plt . $nonce$ ?? queryNonceMetaTagContent ( win . document ) ;
@@ -148,6 +156,7 @@ export const addStyle = (styleContainerNode: any, cmpMeta: d.ComponentRuntimeMet
148156 } else {
149157 styleContainerNode . adoptedStyleSheets = [ stylesheet , ...styleContainerNode . adoptedStyleSheets ] ;
150158 }
159+ appliedStyleElm = null ;
151160 } else {
152161 /**
153162 * If a scoped component is used within a shadow root and constructable stylesheets are
@@ -165,6 +174,7 @@ export const addStyle = (styleContainerNode: any, cmpMeta: d.ComponentRuntimeMet
165174 const existingStyleContainer : HTMLStyleElement = styleContainerNode . querySelector ( 'style' ) ;
166175 if ( existingStyleContainer && ! BUILD . hotModuleReplacement ) {
167176 existingStyleContainer . textContent = style + existingStyleContainer . textContent ;
177+ appliedStyleElm = existingStyleContainer ;
168178 } else {
169179 ( styleContainerNode as HTMLElement ) . prepend ( styleElm ) ;
170180 }
@@ -186,14 +196,12 @@ export const addStyle = (styleContainerNode: any, cmpMeta: d.ComponentRuntimeMet
186196 styleElm . textContent += SLOT_FB_CSS ;
187197 }
188198
189- if ( appliedStyles ) {
190- appliedStyles . add ( scopeId ) ;
191- }
199+ appliedStyles . set ( scopeId , appliedStyleElm ) ;
192200 }
193201 } else if ( BUILD . constructableCSS ) {
194202 let appliedStyles = rootAppliedStyles . get ( styleContainerNode ) ;
195203 if ( ! appliedStyles ) {
196- rootAppliedStyles . set ( styleContainerNode , ( appliedStyles = new Set ( ) ) ) ;
204+ rootAppliedStyles . set ( styleContainerNode , ( appliedStyles = new Map ( ) ) ) ;
197205 }
198206 if ( ! appliedStyles . has ( scopeId ) ) {
199207 /**
@@ -220,7 +228,7 @@ export const addStyle = (styleContainerNode: any, cmpMeta: d.ComponentRuntimeMet
220228 styleContainerNode . adoptedStyleSheets = [ ...styleContainerNode . adoptedStyleSheets , stylesheet ] ;
221229 }
222230
223- appliedStyles . add ( scopeId ) ;
231+ appliedStyles . set ( scopeId , null ) ;
224232
225233 // Remove SSR style element from shadow root now that adoptedStyleSheets is in use
226234 // Only remove from shadow roots, not from document head (for scoped components)
0 commit comments