From 8a28352f1a1508ad7549845e3bf51781ba6ed060 Mon Sep 17 00:00:00 2001 From: xieyuhang <13451213869@163.com> Date: Tue, 16 Aug 2022 14:58:32 +0800 Subject: [PATCH 1/2] fix(hmr): Duplicate link tags --- packages/vite/src/client/client.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/vite/src/client/client.ts b/packages/vite/src/client/client.ts index 0d0deb9347fb11..6d10441ee9637d 100644 --- a/packages/vite/src/client/client.ts +++ b/packages/vite/src/client/client.ts @@ -126,6 +126,7 @@ function cleanUrl(pathname: string): string { } let isFirstUpdate = true +const invalidLinkTags = new Set() async function handleMessage(payload: HMRPayload) { switch (payload.type) { @@ -166,7 +167,10 @@ async function handleMessage(payload: HMRPayload) { // URL for the include check. const el = Array.from( document.querySelectorAll('link') - ).find((e) => cleanUrl(e.href).includes(searchUrl)) + ).find( + (e) => + !invalidLinkTags.has(e) && cleanUrl(e.href).includes(searchUrl) + ) if (el) { const newPath = `${base}${searchUrl.slice(1)}${ searchUrl.includes('?') ? '&' : '?' @@ -179,9 +183,13 @@ async function handleMessage(payload: HMRPayload) { // directly, as the new stylesheet has not yet been loaded. const newLinkTag = el.cloneNode() as HTMLLinkElement newLinkTag.href = new URL(newPath, el.href).href - const removeOldEl = () => el.remove() + const removeOldEl = () => { + el.remove() + invalidLinkTags.delete(el) + } newLinkTag.addEventListener('load', removeOldEl) newLinkTag.addEventListener('error', removeOldEl) + invalidLinkTags.add(el) el.after(newLinkTag) } console.log(`[vite] css hot updated: ${searchUrl}`) From db632ddeb6c9835d8c90afa6320d73b2ac06a2ed Mon Sep 17 00:00:00 2001 From: xieyuhang <13451213869@163.com> Date: Tue, 16 Aug 2022 18:21:39 +0800 Subject: [PATCH 2/2] fix(hmr): code update --- packages/vite/src/client/client.ts | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/packages/vite/src/client/client.ts b/packages/vite/src/client/client.ts index 6d10441ee9637d..dad7c74ee2b8a0 100644 --- a/packages/vite/src/client/client.ts +++ b/packages/vite/src/client/client.ts @@ -126,7 +126,7 @@ function cleanUrl(pathname: string): string { } let isFirstUpdate = true -const invalidLinkTags = new Set() +const outdatedLinkTags = new WeakSet() async function handleMessage(payload: HMRPayload) { switch (payload.type) { @@ -169,7 +169,7 @@ async function handleMessage(payload: HMRPayload) { document.querySelectorAll('link') ).find( (e) => - !invalidLinkTags.has(e) && cleanUrl(e.href).includes(searchUrl) + !outdatedLinkTags.has(e) && cleanUrl(e.href).includes(searchUrl) ) if (el) { const newPath = `${base}${searchUrl.slice(1)}${ @@ -183,13 +183,10 @@ async function handleMessage(payload: HMRPayload) { // directly, as the new stylesheet has not yet been loaded. const newLinkTag = el.cloneNode() as HTMLLinkElement newLinkTag.href = new URL(newPath, el.href).href - const removeOldEl = () => { - el.remove() - invalidLinkTags.delete(el) - } + const removeOldEl = () => el.remove() newLinkTag.addEventListener('load', removeOldEl) newLinkTag.addEventListener('error', removeOldEl) - invalidLinkTags.add(el) + outdatedLinkTags.add(el) el.after(newLinkTag) } console.log(`[vite] css hot updated: ${searchUrl}`)