Fix Fetch+History fragment loading (again)
authorLanius Trolling <lanius@laniustrolling.dev>
Thu, 10 Oct 2024 01:40:39 +0000 (21:40 -0400)
committerLanius Trolling <lanius@laniustrolling.dev>
Thu, 10 Oct 2024 01:40:39 +0000 (21:40 -0400)
src/jvmMain/resources/static/init.js

index b377ef2c92e0c2ea050ff796d1c50fd9be65b1ec..26affe6454074bd64a3a3edf4bb28efaf5a4fa3d 100644 (file)
 
                (async function () {
                        const prevUrl = new URL(window.location.href);
-                       const newState = {"href": url.href, "index": history.state.index + 1};
+                       const newState = {"href": url.href, "hash": url.hash, "index": history.state.index + 1};
 
-                       if (stateMode === "pop" && history.state.href === "") {
+                       if (stateMode === "pop" && history.state.hash === "") {
                                window.scroll(0, history.state.scroll);
                                return;
                        } else if (stateMode !== "pop" && url.pathname === prevUrl.pathname && url.search === prevUrl.search) {
                                        history.replaceState(newState, "", url);
                                }
 
-                               const scrollToElement = document.querySelector(url.hash);
+                               const scrollToElement = url.hash === "" ? null : document.querySelector(url.hash);
                                if (scrollToElement != null) {
                                        scrollToElement.scrollIntoView(true);
                                }
                                        return;
                                }
 
-                               const redirectUrl = new URL(redirectJsonBody.target, window.location.origin)
+                               const redirectUrl = new URL(redirectJsonBody.target, window.location.origin);
                                if (!goToPage(redirectUrl, "push")) {
                                        window.location.href = redirectUrl.href;
                                }
                return true;
        }
 
-       history.replaceState({"href": window.location.href, "index": 0}, "");
+       history.replaceState({"href": window.location.href, "hash": window.location.hash, "index": 0}, "");
 
        let isWindowScrollTicking = false;
        window.addEventListener("scroll", () => {