Skip to content

Commit

Permalink
Merge pull request #2 from deco-cx/refactor-rerender-fn
Browse files Browse the repository at this point in the history
refactor rerender function to improve hot reload
  • Loading branch information
pedrofrxncx authored Oct 31, 2024
2 parents 7a021aa + 886503b commit 22a7da7
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,28 @@ const updateFileCache = async (filepath, content) => {
};

let unmount = () => {};

const rerender = async () => {
unmount();
const entry = await import(
`./app/entry.client.tsx?ts=${Math.floor(Math.random() * 1e3)}`
);
unmount = entry.render();
if (typeof unmount === "function") {
try {
unmount();
} catch (error) {
console.error("Error during unmount:", error);
}
}

try {
const entry = await import(
`./app/entry.client.tsx?ts=${Date.now()}`
);

unmount = typeof entry.render === "function"
? await entry.render()
: () => {};
} catch (error) {
console.error("Error during rerender:", error);
unmount = () => {};
}
};

const registerServiceWorker = async () => {
Expand Down

0 comments on commit 22a7da7

Please sign in to comment.