@@ -495,6 +495,7 @@ async function waitForSuccessfulPingInternal(
495495} 
496496
497497const  sheetsMap  =  new  Map < string ,  HTMLStyleElement > ( ) 
498+ const  linkSheetsMap  =  new  Map < string ,  HTMLLinkElement > ( ) 
498499
499500// collect existing style elements that may have been inserted during SSR 
500501// to avoid FOUC or duplicate styles 
@@ -504,13 +505,22 @@ if ('document' in globalThis) {
504505    . forEach ( ( el )  =>  { 
505506      sheetsMap . set ( el . getAttribute ( 'data-vite-dev-id' ) ! ,  el ) 
506507    } ) 
508+   document 
509+     . querySelectorAll < HTMLLinkElement > ( 
510+       'link[rel="stylesheet"][data-vite-dev-id]' , 
511+     ) 
512+     . forEach ( ( el )  =>  { 
513+       linkSheetsMap . set ( el . getAttribute ( 'data-vite-dev-id' ) ! ,  el ) 
514+     } ) 
507515} 
508516
509517// all css imports should be inserted at the same position 
510518// because after build it will be a single css file 
511519let  lastInsertedStyle : HTMLStyleElement  |  undefined 
512520
513521export  function  updateStyle ( id : string ,  content : string ) : void { 
522+   if  ( linkSheetsMap . has ( id ) )  return 
523+ 
514524  let  style  =  sheetsMap . get ( id ) 
515525  if  ( ! style )  { 
516526    style  =  document . createElement ( 'style' ) 
@@ -540,6 +550,19 @@ export function updateStyle(id: string, content: string): void {
540550} 
541551
542552export  function  removeStyle ( id : string ) : void { 
553+   if  ( linkSheetsMap . has ( id ) )  { 
554+     // re-select elements since HMR can replace links 
555+     document 
556+       . querySelectorAll < HTMLLinkElement > ( 
557+         `link[rel="stylesheet"][data-vite-dev-id]` , 
558+       ) 
559+       . forEach ( ( el )  =>  { 
560+         if  ( el . getAttribute ( 'data-vite-dev-id' )  ===  id )  { 
561+           el . remove ( ) 
562+         } 
563+       } ) 
564+     linkSheetsMap . delete ( id ) 
565+   } 
543566  const  style  =  sheetsMap . get ( id ) 
544567  if  ( style )  { 
545568    document . head . removeChild ( style ) 
0 commit comments