added script for a live reload setup, may develop further in future

This commit is contained in:
Pagwin 2025-04-18 19:48:58 -04:00
parent 249762ecb0
commit 457b9f2b1f

View file

@ -0,0 +1,45 @@
"use strict";
// CREDIT: https://leanrada.com/notes/simple-live-reload
// ACHIVE: https://web.archive.org/web/20250418234708/https://leanrada.com/notes/simple-live-reload/
let watching = new Set();
watch(location.href);
new PerformanceObserver((list) => {
for (const entry of list.getEntries()) {
watch(entry.name);
}
}).observe({ type: "resource", buffered: true });
function watch(urlString) {
if (!urlString) return;
const url = new URL(urlString);
if (url.origin !== location.origin) return;
if (watching.has(url.pathname)) return;
watching.add(url.pathname);
console.log("watching", url.pathname);
let lastModified, etag;
async function check() {
const res = await fetch(url, { method: "head" });
const newLastModified = res.headers.get("Last-Modified");
const newETag = res.headers.get("ETag");
if (
(lastModified !== undefined || etag !== undefined) &&
(lastModified !== newLastModified || etag !== newETag)
) {
location.reload();
}
lastModified = newLastModified;
etag = newETag;
}
setInterval(check, 1000);
}