if (payload.path && payload.path.endsWith('.html')) {
// if html file is edited, only reload the page if the browser is// currently on that page.const pagePath = decodeURI(location.pathname)
const payloadPath = base + payload.path.slice(1)
if (
pagePath === payloadPath ||
payload.path === '/index.html' ||
(pagePath.endsWith('/') && pagePath + 'index.html' === payloadPath)
) {
location.reload()
}
return
} else {
location.reload()
}
payload.updates.forEach((update) => {
if (update.type === 'js-update') {
queueUpdate(fetchUpdate(update))
} else {
// css-update// this is only sent when a css file referenced with <link> is updatedconst { path, timestamp } = update
const searchUrl = cleanUrl(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 = Array.from(
document.querySelectorAll<HTMLLinkElement>('link')
).find((e) =>cleanUrl(e.href).includes(searchUrl))
if (el) {
const newPath = `${base}${searchUrl.slice(1)}${
searchUrl.includes('?') ? '&' : '?'
}t=${timestamp}`// rather than swapping the href on the existing tag, we will// create a new link tag. Once the new stylesheet has loaded we// will remove the existing link tag. This removes a Flash Of// Unstyled Content that can occur when swapping out the tag href// directly, as the new stylesheet has not yet been loaded.const newLinkTag = el.cloneNode() asHTMLLinkElement
newLinkTag.href = newURL(newPath, el.href).hrefconstremoveOldEl = () => el.remove()
newLinkTag.addEventListener('load', removeOldEl)
newLinkTag.addEventListener('error', removeOldEl)
el.after(newLinkTag)
}
console.log(`[vite] css hot updated: ${searchUrl}`)
}
})