Files
aboba/etc/hotreload.js
2025-06-20 14:44:40 +02:00

42 lines
762 B
JavaScript

let build_id = "";
async function fetch_until_ok(uri)
{
while (true) {
try {
return await fetch(uri);
} catch {
}
}
}
async function fetch_build_id()
{
const response = await fetch_until_ok("/build-id");
const json = await response.json();
return json.build_id;
}
function refresh_page()
{
let scroll = localStorage.setItem("scroll", window.scrollY);
location.reload();
}
window.onload = function()
{
setInterval(async function() {
let new_build_id = await fetch_build_id();
if (build_id !== new_build_id) {
refresh_page();
}
}, 1000);
let scroll = localStorage.getItem("scroll");
if (scroll) {
window.scrollTo(0, scroll);
}
}