-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix duplicated Astro and Vite injected styles #8706
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'astro': patch | ||
--- | ||
|
||
Fix duplicated Astro and Vite injected styles |
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,7 @@ | ||
/// <reference types="vite/client" /> | ||
|
||
if (import.meta.hot) { | ||
// Vite injects `<style data-vite-dev-id>` for ESM imports of styles | ||
// but Astro also SSRs with `<style data-astro-dev-id>` blocks. This MutationObserver | ||
// removes any duplicates as soon as they are hydrated client-side. | ||
const injectedStyles = getInjectedStyles(); | ||
const mo = new MutationObserver((records) => { | ||
for (const record of records) { | ||
for (const node of record.addedNodes) { | ||
if (isViteInjectedStyle(node)) { | ||
injectedStyles.get(node.getAttribute('data-vite-dev-id')!)?.remove(); | ||
} | ||
} | ||
} | ||
}); | ||
mo.observe(document.documentElement, { subtree: true, childList: true }); | ||
|
||
// Vue `link` styles need to be manually refreshed in Firefox | ||
import.meta.hot.on('vite:beforeUpdate', async (payload) => { | ||
Comment on lines
-19
to
-20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I manually tested this and it seems to not apply anymore, so I removed it. The rest of this file relates to our CSS handling, which is no longer needed as Vite handles all now, so pretty much the entire file is empty and not need to be loaded anymore. |
||
for (const file of payload.updates) { | ||
if (file.acceptedPath.includes('vue&type=style')) { | ||
const link = document.querySelector(`link[href="${file.acceptedPath}"]`); | ||
if (link) { | ||
link.replaceWith(link.cloneNode(true)); | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
|
||
function getInjectedStyles() { | ||
const injectedStyles = new Map<string, Element>(); | ||
document.querySelectorAll<HTMLStyleElement>('style[data-astro-dev-id]').forEach((el) => { | ||
injectedStyles.set(el.getAttribute('data-astro-dev-id')!, el); | ||
}); | ||
return injectedStyles; | ||
} | ||
|
||
function isStyle(node: Node): node is HTMLStyleElement { | ||
return node.nodeType === node.ELEMENT_NODE && (node as Element).tagName.toLowerCase() === 'style'; | ||
} | ||
|
||
function isViteInjectedStyle(node: Node): node is HTMLStyleElement { | ||
return isStyle(node) && !!node.getAttribute('data-vite-dev-id'); | ||
// HMR temporarily not needed for now, but kept here in case we need it again. | ||
// To re-instate this module again, update `vite-plugin-astro-server/route.ts` | ||
// to add this module as a script similar to `/@vite/client` | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no more
<style data-astro-dev-id
as it's merged into<style data-vite-dev-id
, so this number is reduced by one.