Fix invalid handling of "going back in history" when "Directly go to item in search if there is only one result" setting is set to true

This commit is contained in:
Guillaume Gomez 2023-06-16 16:56:13 +02:00
parent 9f509429cd
commit f4316392a7
2 changed files with 21 additions and 4 deletions

View File

@ -277,14 +277,18 @@ function preLoadCss(cssUrl) {
searchState.mouseMovedAfterSearch = false;
document.title = searchState.title;
},
hideResults: () => {
switchDisplayedElement(null);
removeQueryParameters: () => {
// We change the document title.
document.title = searchState.titleBeforeSearch;
// We also remove the query parameter from the URL.
if (browserSupportsHistoryApi()) {
history.replaceState(null, "", getNakedUrl() + window.location.hash);
}
},
hideResults: () => {
switchDisplayedElement(null);
// We also remove the query parameter from the URL.
searchState.removeQueryParameters();
},
getQueryStringParams: () => {
const params = {};
window.location.search.substring(1).split("&").

View File

@ -2046,6 +2046,20 @@ function initSearch(rawSearchIndex) {
if (go_to_first || (results.others.length === 1
&& getSettingValue("go-to-only-result") === "true")
) {
// Needed to force re-execution of JS when coming back to a page. Let's take this
// scenario as example:
//
// 1. You have the "Directly go to item in search if there is only one result" option
// enabled.
// 2. You make a search which results only one result, leading you automatically to
// this result.
// 3. You go back to previous page.
//
// Now, without the call below, the JS will not be re-executed and the previous state
// will be used, starting search again since the search input is not empty, leading you
// back to the previous page again.
window.onunload = () => {};
searchState.removeQueryParameters();
const elem = document.createElement("a");
elem.href = results.others[0].href;
removeClass(elem, "active");
@ -2182,7 +2196,6 @@ function initSearch(rawSearchIndex) {
if (e) {
e.preventDefault();
}
const query = parseQuery(searchState.input.value.trim());
let filterCrates = getFilterCrates();