How does parcel's HMR work exactly/Questions about parcel's HMR? #9924
Unanswered
ChristophP
asked this question in
Q&A
Replies: 2 comments 4 replies
-
I also found that sometimes instead of hot reloading the page is reloading. When adding the callbacks in a function instead of the module top level. // index.ts
if (module.hot) {
module.hot.dispose(() => console.log('TOP LEVEL DISPOSE'));
module.hot.accept(() => console.log('TOP LEVEL ACCEPT'));
}
function init() {
if (module.hot) {
module.hot.dispose(() => console.log('SCOPED DISPOSE'));
module.hot.accept(() => console.log('SCOPED ACCEPT'));
}
}
init(); When I safe and reload the page I see
but |
Beta Was this translation helpful? Give feedback.
3 replies
-
Here's an example for how you can preserve state between updates: let counter = 0;
if (module.hot) {
module.hot.dispose(function (data) {
// called in the old module before the update. Allows you to store the state that should be preserved
data.counter = counter;
});
module.hot.accept(function () {
// called in the new module after the update. Allows you to reapply the stored state
counter = module.hot.data.counter;
render();
});
}
let btn = document.querySelector("button");
btn.onclick = (e) => {
counter++;
render();
}
function render(){
btn.innerText = \`Update (\${counter})\`;
}
render(); |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi, I am trying to understand how parcel's HMR is working, since it seems differ in some ways to other bundlers implementations.
The docs say this
Specifically, how is state preserved?
Given this redux store definition.
I was first under the impression that only the accept callback will be called but given that parcel reevalueates the entire module (and thus also recreates the store) how does it manage to preserve the state? It works but I do not understand why. Does it reevalue the module but export the store from the previous module?
Would be great to have some info about the details in the docs too.
Beta Was this translation helpful? Give feedback.
All reactions