2023-07-06 17:02:38 -05:00
|
|
|
const cache = {}
|
|
|
|
const main = document.querySelector("html > body > main")
|
|
|
|
const originalMain = main.innerHTML
|
|
|
|
cache[window.location.href] = [originalMain, document.title]
|
|
|
|
history.replaceState([window.location.href, 0, 0], "")
|
|
|
|
|
|
|
|
const MINUTE_MS = 1000 * 60
|
|
|
|
|
|
|
|
async function putCache(href) {
|
|
|
|
if (cache[href] === undefined && href != window.location.href) {
|
|
|
|
cache[href] = ["", ""]
|
|
|
|
console.debug(`Preloading ${href}...`)
|
|
|
|
const html = await (await fetch(href)).text()
|
|
|
|
const doc = new DOMParser().parseFromString(html, "text/html")
|
|
|
|
cache[href] = [doc.querySelector("html > body > main").innerHTML, doc.title]
|
|
|
|
console.debug(`Preloaded ${href}`)
|
|
|
|
setTimeout(() => {
|
|
|
|
delete cache[href]
|
|
|
|
console.debug(`Cleared cached page: ${href}`)
|
|
|
|
}, 5 * MINUTE_MS)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadCache(href) {
|
|
|
|
if (cache[href]) {
|
|
|
|
const [html, title] = cache[href]
|
|
|
|
if (html != "" || title != "") {
|
|
|
|
main.innerHTML = html
|
|
|
|
document.title = title
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
function preloadinate() {
|
|
|
|
// TODO: some kind of expiration?
|
|
|
|
document.querySelectorAll("a").forEach(el => {
|
|
|
|
// TODO: this would ideally happen in the background (service worker)?
|
|
|
|
if (el.href.indexOf("#") == -1 && el.href.indexOf(".") == -1 && (el.href.startsWith("https://lyte.dev") || el.href.startsWith("http://localhost:1313"))) {
|
2023-07-07 10:24:04 -05:00
|
|
|
const router = async function(ev) {
|
2023-07-06 17:02:38 -05:00
|
|
|
if (el.href == window.location.href) {
|
|
|
|
ev.preventDefault()
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if (cache[el.href] && cache[el.href][0] != "") {
|
|
|
|
const rep = [window.location.href, window.scrollX, window.scrollY]
|
|
|
|
history.replaceState(rep, "")
|
|
|
|
console.log("replaceState: ", rep)
|
|
|
|
if (!loadCache(el.href)) {
|
2023-07-06 17:11:20 -05:00
|
|
|
window.location.href = el.href
|
|
|
|
window.location.reload()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
window.scrollTo(0, 0)
|
|
|
|
history.pushState([el.href, 0, 0], "", el.href)
|
|
|
|
preloadinate()
|
|
|
|
ev.preventDefault()
|
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
ev.preventDefault()
|
|
|
|
await putCache(el.href)
|
2023-07-07 10:24:04 -05:00
|
|
|
return router(ev)
|
2023-07-06 17:02:38 -05:00
|
|
|
}
|
2023-07-07 10:24:04 -05:00
|
|
|
}
|
|
|
|
el.addEventListener("mouseover", (_ev) => putCache(el.href))
|
|
|
|
el.addEventListener("click", router)
|
2023-07-06 17:02:38 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
window.addEventListener("popstate", ev => {
|
|
|
|
if (ev.state) {
|
|
|
|
const [href, x, y] = ev.state
|
|
|
|
if (!loadCache(href)) {
|
|
|
|
window.location.reload()
|
|
|
|
}
|
|
|
|
requestAnimationFrame(() => {
|
|
|
|
console.log(`Scrolling to ${x}, ${y}`)
|
|
|
|
window.scrollTo(x, y)
|
|
|
|
})
|
|
|
|
preloadinate()
|
|
|
|
} else {
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
window.addEventListener("load", preloadinate);
|