(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 === "full") {
location.reload();
return;
}
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;
Array.from(newDoc.body.attributes).forEach((attr) => {
document.body.setAttribute(attr.name, attr.value);
});
Array.from(document.body.attributes).forEach((attr) => {
if (!newDoc.body.hasAttribute(attr.name)) {
document.body.removeAttribute(attr.name);
}
});
Array.from(newDoc.documentElement.attributes).forEach((attr) => {
document.documentElement.setAttribute(attr.name, attr.value);
});
Array.from(document.documentElement.attributes).forEach((attr) => {
if (!newDoc.documentElement.hasAttribute(attr.name)) {
document.documentElement.removeAttribute(attr.name);
}
});
if (newDoc.title !== document.title) {
document.title = newDoc.title;
}
const cacheBuster = Date.now();
document.querySelectorAll('link[rel="stylesheet"]').forEach((link) => {
const href = link.getAttribute("href");
if (href) {
const url = new URL(href, location.origin);
url.searchParams.set("_t", cacheBuster);
link.setAttribute("href", url.toString());
}
});
requestAnimationFrame(() => {
if (typeof window.__hydrateIslands === "function") {
window.__hydrateIslands();
}
});
} catch (e) {
location.reload();
}
}
};
ws.onclose = function () {
setTimeout(function () {
location.reload();
}, 1000);
};
})();