From da8647fc2924b1892e77f91d0f3c7b65ed39e22a Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Thu, 6 Jul 2023 17:11:20 -0500 Subject: [PATCH] All roads lead to preload --- static/preload.mjs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/static/preload.mjs b/static/preload.mjs index a40fd79..0fe41d1 100644 --- a/static/preload.mjs +++ b/static/preload.mjs @@ -39,7 +39,7 @@ function preloadinate() { // 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"))) { el.addEventListener("mouseover", (_ev) => putCache(el.href)) - el.addEventListener("click", ev => { + el.addEventListener("click", async (ev) => { if (el.href == window.location.href) { ev.preventDefault() return false @@ -49,6 +49,24 @@ function preloadinate() { history.replaceState(rep, "") console.log("replaceState: ", rep) if (!loadCache(el.href)) { + 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) + const rep = [window.location.href, window.scrollX, window.scrollY] + history.replaceState(rep, "") + console.log("replaceState: ", rep) + if (!loadCache(el.href)) { + window.location.href = el.href + window.location.reload() return true } window.scrollTo(0, 0)