Skip to content

Commit 27f875a

Browse files
committed
rustdoc-search: fix back/forward interaction after reload
1 parent 7b06127 commit 27f875a

File tree

2 files changed

+58
-52
lines changed

2 files changed

+58
-52
lines changed

src/librustdoc/html/static/js/main.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,64 @@ function preLoadCss(cssUrl) {
377377
};
378378
}
379379

380+
// Push and pop states are used to add search results to the browser
381+
// history.
382+
if (browserSupportsHistoryApi()) {
383+
// Store the previous <title> so we can revert back to it later.
384+
const previousTitle = document.title;
385+
386+
window.addEventListener("popstate", e => {
387+
const params = searchState.getQueryStringParams();
388+
// Revert to the previous title manually since the History
389+
// API ignores the title parameter.
390+
document.title = previousTitle;
391+
// Synchronize search bar with query string state and
392+
// perform the search. This will empty the bar if there's
393+
// nothing there, which lets you really go back to a
394+
// previous state with nothing in the bar.
395+
if (params.search !== undefined) {
396+
loadSearch();
397+
searchState.inputElement().value = params.search;
398+
// Some browsers fire "onpopstate" for every page load
399+
// (Chrome), while others fire the event only when actually
400+
// popping a state (Firefox), which is why search() is
401+
// called both here and at the end of the startSearch()
402+
// function.
403+
e.preventDefault();
404+
searchState.showResults();
405+
if (params.search === "") {
406+
searchState.focus();
407+
}
408+
} else {
409+
// When browsing back from search results the main page
410+
// visibility must be reset.
411+
searchState.hideResults();
412+
}
413+
});
414+
}
415+
416+
// This is required in firefox to avoid this problem: Navigating to a search result
417+
// with the keyboard, hitting enter, and then hitting back would take you back to
418+
// the doc page, rather than the search that should overlay it.
419+
// This was an interaction between the back-forward cache and our handlers
420+
// that try to sync state between the URL and the search input. To work around it,
421+
// do a small amount of re-init on page show.
422+
window.onpageshow = () => {
423+
const qSearch = searchState.getQueryStringParams().search;
424+
if (qSearch !== undefined) {
425+
if (searchState.inputElement().value === "") {
426+
searchState.inputElement().value = qSearch;
427+
}
428+
searchState.showResults();
429+
if (qSearch === "") {
430+
loadSearch();
431+
searchState.focus();
432+
}
433+
} else {
434+
searchState.hideResults();
435+
}
436+
};
437+
380438
const params = searchState.getQueryStringParams();
381439
if (params.search !== undefined) {
382440
searchState.setLoadingSearch();

src/librustdoc/html/static/js/search.js

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4819,58 +4819,6 @@ function registerSearchEvents() {
48194819
searchState.inputElement().addEventListener("blur", () => {
48204820
searchState.inputElement().placeholder = searchState.inputElement().origPlaceholder;
48214821
});
4822-
4823-
// Push and pop states are used to add search results to the browser
4824-
// history.
4825-
if (browserSupportsHistoryApi()) {
4826-
// Store the previous <title> so we can revert back to it later.
4827-
const previousTitle = document.title;
4828-
4829-
window.addEventListener("popstate", e => {
4830-
const params = searchState.getQueryStringParams();
4831-
// Revert to the previous title manually since the History
4832-
// API ignores the title parameter.
4833-
document.title = previousTitle;
4834-
// When browsing forward to search results the previous
4835-
// search will be repeated, so the currentResults are
4836-
// cleared to ensure the search is successful.
4837-
currentResults = null;
4838-
// Synchronize search bar with query string state and
4839-
// perform the search. This will empty the bar if there's
4840-
// nothing there, which lets you really go back to a
4841-
// previous state with nothing in the bar.
4842-
if (params.search !== undefined) {
4843-
searchState.inputElement().value = params.search;
4844-
// Some browsers fire "onpopstate" for every page load
4845-
// (Chrome), while others fire the event only when actually
4846-
// popping a state (Firefox), which is why search() is
4847-
// called both here and at the end of the startSearch()
4848-
// function.
4849-
e.preventDefault();
4850-
search();
4851-
} else {
4852-
// When browsing back from search results the main page
4853-
// visibility must be reset.
4854-
searchState.hideResults();
4855-
}
4856-
});
4857-
}
4858-
4859-
// This is required in firefox to avoid this problem: Navigating to a search result
4860-
// with the keyboard, hitting enter, and then hitting back would take you back to
4861-
// the doc page, rather than the search that should overlay it.
4862-
// This was an interaction between the back-forward cache and our handlers
4863-
// that try to sync state between the URL and the search input. To work around it,
4864-
// do a small amount of re-init on page show.
4865-
window.onpageshow = () => {
4866-
const qSearch = searchState.getQueryStringParams().search;
4867-
if (qSearch !== undefined) {
4868-
if (searchState.inputElement().value === "") {
4869-
searchState.inputElement().value = qSearch;
4870-
}
4871-
search();
4872-
}
4873-
};
48744822
}
48754823

48764824
function updateCrate(ev) {

0 commit comments

Comments
 (0)