Hot reloading index.html? #20233
Unanswered
simonbuchan
asked this question in
Q&A
Replies: 1 comment
-
So far I have a workaround based on throwing from if (import.meta.hot) {
const preventHotReload = Symbol("prevent-hot-reload");
import.meta.on("vite:beforeFullReload", ({ path }) => {
if (path?.endsWith(".html")) {
console.info("preventing hot reload");
throw preventHotReload;
}
});
// suppress error message
addEventListener("unhandledrejection", (event) => {
if (event.reason === preventHotReload) {
event.preventDefault();
}
});
// hot reload html
import.meta.on("my:html-update", ({ content }) => {
// dumbass approach
const newDocument = new DOMParser().parseFromString(content, "text/html");
document.body.replaceWith(newDocument.body);
});
// hot reload hooks...
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I'm doing this trick in a prototype:
Which works shockingly well with minimal code, just a
MutationObserver
and aroot.querySelectorAll("not:(template) [data-hook]")
and a bit of cleanup handling really, except that if I start messing with the HTML Vite (reasonably) reloads the whole page, where in this case it could simply swap out the content with something likedocument.replaceChildren()
modulo script tags and the like.The
handleHotReload
plugin hook does get.html
updates, but returning[]
doesn't disable thevite:full-reload
event getting sent so this turns into #5763 . Is there any other option I'm missing here?Beta Was this translation helpful? Give feedback.
All reactions