Skip to content

Commit 51a14c7

Browse files
committed
Fix back-forward cache in rustdoc frontend.
Rustdoc's frontend set a no-op unload handler, specifically to disable Firefox's back-forward cache because it caused a bug. It's nice to allow the back-forward cache because it permits faster navigations. This change addresses the issues that were caused by back-forward cache. https://developer.mozilla.org/en-US/docs/Mozilla/Firefox/Releases/1.5/Using_Firefox_1.5_caching https://web.dev/bfcache/
1 parent 9b471a3 commit 51a14c7

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

src/librustdoc/html/static/main.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -1995,6 +1995,20 @@ function defocusSearchBar() {
19951995
});
19961996
}
19971997
search();
1998+
1999+
// This is required in firefox to avoid this problem: Navigating to a search result
2000+
// with the keyboard, hitting enter, and then hitting back would take you back to
2001+
// the doc page, rather than the search that should overlay it.
2002+
// This was an interaction between the back-forward cache and our handlers
2003+
// that try to sync state between the URL and the search input. To work around it,
2004+
// do a small amount of re-init on page show.
2005+
window.onpageshow = function(){
2006+
var qSearch = getQueryStringParams().search;
2007+
if (search_input.value === "" && qSearch) {
2008+
search_input.value = qSearch;
2009+
}
2010+
search();
2011+
};
19982012
}
19992013

20002014
index = buildIndex(rawSearchIndex);
@@ -2953,9 +2967,3 @@ function defocusSearchBar() {
29532967
onHashChange(null);
29542968
window.onhashchange = onHashChange;
29552969
}());
2956-
2957-
// This is required in firefox. Explanations: when going back in the history, firefox doesn't re-run
2958-
// the JS, therefore preventing rustdoc from setting a few things required to be able to reload the
2959-
// previous search results (if you navigated to a search result with the keyboard, pressed enter on
2960-
// it to navigate to that result, and then came back to this page).
2961-
window.onunload = function(){};

0 commit comments

Comments
 (0)