Skip to content

Commit 0846043

Browse files
committed
Auto merge of #82511 - jsha:fix-bfcache2, r=GuillaumeGomez
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/ Demo: https://jacob.hoffman-andrews.com/rust/fix-bfcache/std/string/struct.String.html Related: #72272
2 parents 9fa580b + 51a14c7 commit 0846043

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
@@ -2000,6 +2000,20 @@ function defocusSearchBar() {
20002000
});
20012001
}
20022002
search();
2003+
2004+
// This is required in firefox to avoid this problem: Navigating to a search result
2005+
// with the keyboard, hitting enter, and then hitting back would take you back to
2006+
// the doc page, rather than the search that should overlay it.
2007+
// This was an interaction between the back-forward cache and our handlers
2008+
// that try to sync state between the URL and the search input. To work around it,
2009+
// do a small amount of re-init on page show.
2010+
window.onpageshow = function(){
2011+
var qSearch = getQueryStringParams().search;
2012+
if (search_input.value === "" && qSearch) {
2013+
search_input.value = qSearch;
2014+
}
2015+
search();
2016+
};
20032017
}
20042018

20052019
index = buildIndex(rawSearchIndex);
@@ -2958,9 +2972,3 @@ function defocusSearchBar() {
29582972
onHashChange(null);
29592973
window.onhashchange = onHashChange;
29602974
}());
2961-
2962-
// This is required in firefox. Explanations: when going back in the history, firefox doesn't re-run
2963-
// the JS, therefore preventing rustdoc from setting a few things required to be able to reload the
2964-
// previous search results (if you navigated to a search result with the keyboard, pressed enter on
2965-
// it to navigate to that result, and then came back to this page).
2966-
window.onunload = function(){};

0 commit comments

Comments
 (0)