From 0ad01b5a00903ee67ae4096294dee4b07a14cb9a Mon Sep 17 00:00:00 2001 From: Lanius Trolling Date: Thu, 26 Dec 2024 13:11:59 -0500 Subject: [PATCH] Fix back button --- src/main/resources/static/init.js | 54 ++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/src/main/resources/static/init.js b/src/main/resources/static/init.js index 764a90a..253018b 100644 --- a/src/main/resources/static/init.js +++ b/src/main/resources/static/init.js @@ -88,6 +88,21 @@ || pathName.startsWith("/user"); } + /** + * @property {string} pathname + * @property {string} search + * @property {function():void} update + * @type {Object} + */ + const loadedPage = { + pathname: window.location.pathname, + search: window.location.search, + update() { + this.pathname = window.location.pathname; + this.search = window.location.search; + } + }; + /** * @param {URL} url * @param {string} stateMode @@ -100,25 +115,25 @@ } (async function () { - const prevUrl = new URL(window.location.href); const newState = {"href": url.href, "hash": url.hash, "index": history.state.index + 1}; - if (stateMode === "pop" && history.state.hash === "") { - window.scroll(0, history.state.scroll); - return; - } else if (stateMode !== "pop" && formData == null && url.pathname === prevUrl.pathname && url.search === prevUrl.search) { - newState.href = ""; - - if (stateMode === "push") { - history.replaceState({...history.state, "scroll": window.scrollY}, ""); - history.pushState(newState, "", url); - } else if (stateMode === "replace") { - history.replaceState(newState, "", url); - } + if (formData == null && url.pathname === loadedPage.pathname && url.search === loadedPage.search) { + if (stateMode === "pop") { + window.scroll(0, history.state.scroll); + } else { + newState.href = ""; - const scrollToElement = url.hash === "" ? null : document.querySelector(url.hash); - if (scrollToElement != null) { - scrollToElement.scrollIntoView(true); + if (stateMode === "push") { + history.replaceState({...history.state, "scroll": window.scrollY}, ""); + history.pushState(newState, "", url); + } else if (stateMode === "replace") { + history.replaceState(newState, "", url); + } + + const scrollToElement = url.hash === "" ? null : document.querySelector(url.hash); + if (scrollToElement != null) { + scrollToElement.scrollIntoView(true); + } } return; @@ -173,9 +188,12 @@ replaceElement(document.body, htmlDocument.body); onDomLoad(document.body); + + loadedPage.update(); + if (stateMode === "pop") { window.scroll(0, history.state.scroll); - } else if (url.hash !== '') { + } else if (url.hash !== "") { const scrollToElement = document.querySelector(url.hash); if (scrollToElement != null) { scrollToElement.scrollIntoView(true); @@ -190,7 +208,7 @@ return true; } - history.replaceState({"href": window.location.href, "hash": window.location.hash, "index": 0}, ""); + history.replaceState({...(history.state ?? {"index": 0}), "href": window.location.href, "hash": window.location.hash}, ""); let isWindowScrollTicking = false; window.addEventListener("scroll", () => { -- 2.25.1