Skip to content

Commit

Permalink
fix(hmr): preserve host when updating link CSS
Browse files Browse the repository at this point in the history
fix #1665
  • Loading branch information
yyx990803 committed Jan 24, 2021
1 parent 46bb853 commit 60f9782
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions packages/vite/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,17 @@ async function handleMessage(payload: HMRPayload) {
// this is only sent when a css file referenced with <link> is updated
let { path, timestamp } = update
path = path.replace(/\?.*/, '')
const el = document.querySelector(`link[href*='${path}']`)
// can't use querySelector with `[href*=]` here since the link may be
// using relative paths so we need to use link.href to grab the full
// URL for the include check.
const el = ([].slice.call(
document.querySelectorAll(`link`)
) as HTMLLinkElement[]).find((e) => e.href.includes(path))
if (el) {
el.setAttribute(
'href',
`${path}${path.includes('?') ? '&' : '?'}t=${timestamp}`
)
const newPath = `${path}${
path.includes('?') ? '&' : '?'
}t=${timestamp}`
el.href = new URL(newPath, el.href).href
}
console.log(`[vite] css hot updated: ${path}`)
}
Expand Down

0 comments on commit 60f9782

Please sign in to comment.