(function () {
const ws = new WebSocket("ws://localhost:3001/__reload");
async function fetchWithRetry(url, maxRetries = 50, delay = 100) {
for (let i = 0; i < maxRetries; i++) {
try {
const response = await fetch(url);
if (response.ok) return response;
} catch (e) {}
await new Promise((r) => setTimeout(r, delay));
}
throw new Error("Server not ready");
}
ws.onmessage = async function (event) {
if (event.data === "reload") {
try {
const response = await fetchWithRetry(location.href);
const html = await response.text();
const parser = new DOMParser();
const newDoc = parser.parseFromString(html, "text/html");
document.body.innerHTML = newDoc.body.innerHTML;
if (newDoc.title !== document.title) {
document.title = newDoc.title;
}
} catch (e) {
location.reload();
}
}
};
ws.onclose = function () {
setTimeout(function () {
location.reload();
}, 1000);
};
})();