Skip to content

Commit 64ab6db

Browse files
committed
fix(history): popstate goes to nothing
1 parent c85bfdc commit 64ab6db

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

static/js/search.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ function searchNoRealod(searchQuery) {
1010
searchInputEl.blur();
1111
const query = encodeURIComponent(searchQuery);
1212
document.getElementById("search-suggestions").style.display = "none";
13-
window.history.pushState({}, "", `/search?query=${query}`);
13+
const prevPath = window.location.href.substring(
14+
(window.location.protocol + "//" + window.location.host).length,
15+
);
16+
window.location.prevPath = prevPath;
17+
window.history.pushState({ prevPath }, "", `/search?query=${query}`);
1418
}
1519

1620
searchFormEl.addEventListener("submit", (e) => {

static/js/utils.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ function getTextWidth() {
3838
return window.innerWidth > 768 ? 35 : 15;
3939
}
4040

41+
window.addEventListener("popstate", (e) => {
42+
e.stopImmediatePropagation();
43+
e.preventDefault();
44+
if (history.state && history.state.prevPath) {
45+
window.open(history.state.prevPath, "_self");
46+
} else {
47+
window.open(window.location.prevPath, "_self");
48+
}
49+
});
50+
4151
window.Utils = {
4252
toggleLoading,
4353
formatTime,

views/components/navlink/navlink.templ

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,7 @@ templ NavLink(title, imageUrl, path string) {
1717
}
1818

1919
script updateHref(path string) {
20-
window.history.pushState({}, "", path)
20+
const prevPath = window.location.href.substring((window.location.protocol+"//"+window.location.host).length);
21+
window.location.prevPath = prevPath;
22+
window.history.pushState({ prevPath }, "", path)
2123
}

0 commit comments

Comments
 (0)