11/// <reference types="vite/client" />
22
33if ( import . meta. hot ) {
4- // Vite injects `<style type="text/css">` for ESM imports of styles
5- // but Astro also SSRs with `<style>` blocks. This MutationObserver
4+ // Vite injects `<style type="text/css" data-vite-dev-id >` for ESM imports of styles
5+ // but Astro also SSRs with `<style type="text/css" data-astro-dev-id >` blocks. This MutationObserver
66 // removes any duplicates as soon as they are hydrated client-side.
77 const injectedStyles = getInjectedStyles ( ) ;
88 const mo = new MutationObserver ( ( records ) => {
99 for ( const record of records ) {
1010 for ( const node of record . addedNodes ) {
1111 if ( isViteInjectedStyle ( node ) ) {
12- injectedStyles . get ( node . innerHTML . trim ( ) ) ?. remove ( ) ;
12+ injectedStyles . get ( node . getAttribute ( 'data-vite-dev-id' ) ! ) ?. remove ( ) ;
1313 }
1414 }
1515 }
@@ -31,8 +31,8 @@ if (import.meta.hot) {
3131
3232function getInjectedStyles ( ) {
3333 const injectedStyles = new Map < string , Element > ( ) ;
34- document . querySelectorAll < HTMLStyleElement > ( 'style' ) . forEach ( ( el ) => {
35- injectedStyles . set ( el . innerHTML . trim ( ) , el ) ;
34+ document . querySelectorAll < HTMLStyleElement > ( 'style[data-astro-dev-id] ' ) . forEach ( ( el ) => {
35+ injectedStyles . set ( el . getAttribute ( 'data-astro-dev-id' ) ! , el ) ;
3636 } ) ;
3737 return injectedStyles ;
3838}
@@ -42,5 +42,5 @@ function isStyle(node: Node): node is HTMLStyleElement {
4242}
4343
4444function isViteInjectedStyle ( node : Node ) : node is HTMLStyleElement {
45- return isStyle ( node ) && node . getAttribute ( 'type' ) === 'text/css' ;
45+ return isStyle ( node ) && node . getAttribute ( 'type' ) === 'text/css' && ! ! node . getAttribute ( 'data-vite-dev-id' ) ;
4646}
0 commit comments