@@ -16,6 +16,7 @@ import { createTime } from './profile';
1616import { HYDRATED_STYLE_ID , NODE_TYPE , SLOT_FB_CSS } from './runtime-constants' ;
1717
1818export const rootAppliedStyles : d . RootAppliedStyleMap = /*@__PURE__ */ new WeakMap ( ) ;
19+ export const rootAppliedStyleElms : WeakMap < any , Map < string , HTMLStyleElement > > = /*@__PURE__ */ new WeakMap ( ) ;
1920
2021/**
2122 * Register the styles for a component by creating a stylesheet and then
@@ -71,24 +72,39 @@ export const addStyle = (styleContainerNode: any, cmpMeta: d.ComponentRuntimeMet
7172 if ( typeof style === 'string' ) {
7273 styleContainerNode = styleContainerNode . head || ( styleContainerNode as HTMLElement ) ;
7374 let appliedStyles = rootAppliedStyles . get ( styleContainerNode ) ;
75+ let appliedStyleElms = rootAppliedStyleElms . get ( styleContainerNode ) ;
7476 let styleElm ;
7577 if ( ! appliedStyles ) {
7678 rootAppliedStyles . set ( styleContainerNode , ( appliedStyles = new Set ( ) ) ) ;
7779 }
80+ if ( ! appliedStyleElms ) {
81+ rootAppliedStyleElms . set ( styleContainerNode , ( appliedStyleElms = new Map ( ) ) ) ;
82+ }
83+
84+ const trackedStyleElm = appliedStyleElms . get ( scopeId ) ;
85+ if ( trackedStyleElm && trackedStyleElm . parentNode !== styleContainerNode ) {
86+ appliedStyles . delete ( scopeId ) ;
87+ appliedStyleElms . delete ( scopeId ) ;
88+ }
7889
7990 // Check if style element already exists (for HMR updates)
8091 // For shadow DOM components, directly update their dedicated style element
8192 // 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 } "]` ) ;
93+ const existingStyleElm : HTMLStyleElement | undefined =
94+ ( trackedStyleElm ?. parentNode === styleContainerNode && trackedStyleElm ) ||
95+ ( ( BUILD . hydrateClientSide || BUILD . hotModuleReplacement ) &&
96+ styleContainerNode . querySelector ( `[${ HYDRATED_STYLE_ID } ="${ scopeId } "]` ) ) ||
97+ undefined ;
8598
8699 if ( existingStyleElm ) {
87100 // Update existing style element (for hydration or HMR)
88101 existingStyleElm . textContent = style ;
102+ appliedStyles . add ( scopeId ) ;
103+ appliedStyleElms . set ( scopeId , existingStyleElm ) ;
89104 } else if ( ! appliedStyles . has ( scopeId ) ) {
90105 styleElm = win . document . createElement ( 'style' ) ;
91106 styleElm . textContent = style ;
107+ let appliedStyleElm = styleElm ;
92108
93109 // Apply CSP nonce to the style tag if it exists
94110 const nonce = plt . $nonce$ ?? queryNonceMetaTagContent ( win . document ) ;
@@ -165,6 +181,7 @@ export const addStyle = (styleContainerNode: any, cmpMeta: d.ComponentRuntimeMet
165181 const existingStyleContainer : HTMLStyleElement = styleContainerNode . querySelector ( 'style' ) ;
166182 if ( existingStyleContainer && ! BUILD . hotModuleReplacement ) {
167183 existingStyleContainer . textContent = style + existingStyleContainer . textContent ;
184+ appliedStyleElm = existingStyleContainer ;
168185 } else {
169186 ( styleContainerNode as HTMLElement ) . prepend ( styleElm ) ;
170187 }
@@ -189,6 +206,7 @@ export const addStyle = (styleContainerNode: any, cmpMeta: d.ComponentRuntimeMet
189206 if ( appliedStyles ) {
190207 appliedStyles . add ( scopeId ) ;
191208 }
209+ appliedStyleElms . set ( scopeId , appliedStyleElm ) ;
192210 }
193211 } else if ( BUILD . constructableCSS ) {
194212 let appliedStyles = rootAppliedStyles . get ( styleContainerNode ) ;
0 commit comments