diff --git a/.changeset/large-beds-cheer.md b/.changeset/large-beds-cheer.md new file mode 100644 index 000000000000..10a3aecffc78 --- /dev/null +++ b/.changeset/large-beds-cheer.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix duplicated CSS when using HMR diff --git a/packages/astro/src/core/render/dev/index.ts b/packages/astro/src/core/render/dev/index.ts index 7de9a979a45f..abce2c4e4f60 100644 --- a/packages/astro/src/core/render/dev/index.ts +++ b/packages/astro/src/core/render/dev/index.ts @@ -148,8 +148,7 @@ export async function render( links.add({ props: { rel: 'stylesheet', - href, - 'data-astro-injected': true, + href }, children: '', }); @@ -162,15 +161,12 @@ export async function render( props: { type: 'module', src: url, - 'data-astro-injected': true, }, children: '', }); // But we still want to inject the styles to avoid FOUC styles.add({ - props: { - 'data-astro-injected': url, - }, + props: {}, children: content, }); }); diff --git a/packages/astro/src/runtime/client/hmr.ts b/packages/astro/src/runtime/client/hmr.ts index ba17fc526b6c..f3a3074f3a46 100644 --- a/packages/astro/src/runtime/client/hmr.ts +++ b/packages/astro/src/runtime/client/hmr.ts @@ -1,21 +1,24 @@ /// + if (import.meta.hot) { - import.meta.hot.on('vite:beforeUpdate', async (payload) => { - for (const file of payload.updates) { - if ( - file.acceptedPath.includes('svelte&type=style') || - file.acceptedPath.includes('astro&type=style') - ) { - // This will only be called after the svelte component has hydrated in the browser. - // At this point Vite is tracking component style updates, we need to remove - // styles injected by Astro for the component in favor of Vite's internal HMR. - const injectedStyle = document.querySelector( - `style[data-astro-injected="${file.acceptedPath}"]` - ); - if (injectedStyle) { - injectedStyle.parentElement?.removeChild(injectedStyle); + // Vite injects `