-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve reload experience when using with site generator such as cargo
doc
- Loading branch information
1 parent
bcda1df
commit f6e77f8
Showing
7 changed files
with
215 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const meta = document.createElement("meta"); | ||
meta.name = "live-server"; | ||
meta.content = "reload"; | ||
document.head.appendChild(meta); | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
(async (addr, hard) => { | ||
addr = `ws://${addr}/live-server-ws`; | ||
const sleep = (x) => new Promise(r => setTimeout(r, x)); | ||
const preload = async (url, requireSuccess) => { | ||
const resp = await fetch(url, {cache: "reload"}); // reset cache | ||
if (requireSuccess && (!resp.ok || resp.status !== 200)) { throw new Error(); } | ||
} | ||
/** Reset cache in link.href and strip scripts */ | ||
const preloadNode = (n, ps) => { | ||
if (n.tagName === "SCRIPT" && n.src) { ps.push(preload(n.src, false)); return; } | ||
if (n.tagName === "LINK" && n.href) { ps.push(preload(n.href, false)); return; } | ||
let c = n.firstChild; while (c) { let nc = c.nextSibling; preloadNode(c, ps); c = nc; } | ||
}; | ||
let reloading = false; | ||
let scheduled = false; | ||
async function reload() { | ||
if(reloading) { | ||
scheduled = true; | ||
return; | ||
} | ||
let ifr; | ||
let interval = 0; | ||
reloading = true; | ||
while(true) { | ||
scheduled = false; | ||
try { | ||
const url = location.origin + location.pathname; | ||
const ps = []; | ||
preloadNode(document.head, ps); | ||
preloadNode(document.body, ps); | ||
await Promise.allSettled(ps); | ||
await new Promise((resolve) => { | ||
ifr = document.createElement("iframe"); | ||
ifr.src = url + "?reload"; | ||
ifr.style.display = "none"; | ||
ifr.onload = resolve; | ||
document.body.appendChild(ifr); | ||
}); | ||
const meta = ifr.contentDocument.head.lastChild; | ||
if (meta.tagName !== "META" || meta.name !== "live-server" || meta.content !== "reload") { | ||
throw new Error(); | ||
} | ||
if (hard) { | ||
location.reload(); | ||
} | ||
document.head.replaceWith(ifr.contentDocument.head); | ||
document.body.replaceWith(ifr.contentDocument.body); | ||
ifr.remove(); | ||
if (!scheduled) { | ||
reloading = false; | ||
console.log("[Live Server] Reloaded"); | ||
return; | ||
} | ||
} catch (e) { | ||
if (e.message) { console.error(e); } | ||
} | ||
if (ifr) { ifr.remove(); } | ||
interval += 100; | ||
await sleep(interval); | ||
} | ||
} | ||
// connection | ||
let isFirst = true; | ||
let interval = 0; | ||
while (true) { | ||
try { | ||
await new Promise((resolve) => { | ||
const ws = new WebSocket(addr); | ||
ws.onopen = ()=>{ | ||
interval = 0; | ||
console.log("[Live Server] Connection Established"); | ||
if (!isFirst) { | ||
reload(); | ||
} | ||
}; | ||
ws.onmessage = reload; | ||
ws.onerror = () =>ws.close(); | ||
ws.onclose = resolve; | ||
}); | ||
} catch (e) { | ||
} | ||
isFirst = false; | ||
interval += 500; | ||
await sleep(interval); | ||
console.log("[Live Server] Reconnecting..."); | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters