Skip to content

Commit

Permalink
fix(hmr): duplicate link tags (#9697)
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyhn committed Aug 16, 2022
1 parent d9eb6b9 commit 9aa9515
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/vite/src/client/client.ts
Expand Up @@ -126,6 +126,7 @@ function cleanUrl(pathname: string): string {
}

let isFirstUpdate = true
const outdatedLinkTags = new WeakSet<HTMLLinkElement>()

async function handleMessage(payload: HMRPayload) {
switch (payload.type) {
Expand Down Expand Up @@ -166,7 +167,10 @@ async function handleMessage(payload: HMRPayload) {
// URL for the include check.
const el = Array.from(
document.querySelectorAll<HTMLLinkElement>('link')
).find((e) => cleanUrl(e.href).includes(searchUrl))
).find(
(e) =>
!outdatedLinkTags.has(e) && cleanUrl(e.href).includes(searchUrl)
)
if (el) {
const newPath = `${base}${searchUrl.slice(1)}${
searchUrl.includes('?') ? '&' : '?'
Expand All @@ -182,6 +186,7 @@ async function handleMessage(payload: HMRPayload) {
const removeOldEl = () => el.remove()
newLinkTag.addEventListener('load', removeOldEl)
newLinkTag.addEventListener('error', removeOldEl)
outdatedLinkTags.add(el)
el.after(newLinkTag)
}
console.log(`[vite] css hot updated: ${searchUrl}`)
Expand Down

0 comments on commit 9aa9515

Please sign in to comment.