Fix back button
authorLanius Trolling <lanius@laniustrolling.dev>
Thu, 26 Dec 2024 18:11:59 +0000 (13:11 -0500)
committerLanius Trolling <lanius@laniustrolling.dev>
Thu, 26 Dec 2024 18:11:59 +0000 (13:11 -0500)
src/main/resources/static/init.js

index 764a90a0ddd184f9806a59d80e23d04e86e8ed5c..253018b001acd4c8b6ad9b79e9df4338deb2f514 100644 (file)
                        || 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
                }
 
                (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;
                        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);
                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", () => {