diff options
| author | Jacob Hoffman-Andrews <github@hoffman-andrews.com> | 2021-02-24 19:31:07 -0800 |
|---|---|---|
| committer | Jacob Hoffman-Andrews <github@hoffman-andrews.com> | 2021-02-24 22:16:26 -0800 |
| commit | 51a14c7265971d4a2ab93d5aca82eb0d4261c034 (patch) | |
| tree | d98a832123212195bf6c2ee3f8fbd480c57de318 | |
| parent | 9b471a3f5fe57e5c6e08acf665f2094422415a3d (diff) | |
| download | rust-51a14c7265971d4a2ab93d5aca82eb0d4261c034.tar.gz rust-51a14c7265971d4a2ab93d5aca82eb0d4261c034.zip | |
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/
| -rw-r--r-- | src/librustdoc/html/static/main.js | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js index d6d3171afbf..0f8bad10874 100644 --- a/src/librustdoc/html/static/main.js +++ b/src/librustdoc/html/static/main.js @@ -1995,6 +1995,20 @@ function defocusSearchBar() { }); } search(); + + // This is required in firefox to avoid this problem: Navigating to a search result + // with the keyboard, hitting enter, and then hitting back would take you back to + // the doc page, rather than the search that should overlay it. + // This was an interaction between the back-forward cache and our handlers + // that try to sync state between the URL and the search input. To work around it, + // do a small amount of re-init on page show. + window.onpageshow = function(){ + var qSearch = getQueryStringParams().search; + if (search_input.value === "" && qSearch) { + search_input.value = qSearch; + } + search(); + }; } index = buildIndex(rawSearchIndex); @@ -2953,9 +2967,3 @@ function defocusSearchBar() { onHashChange(null); window.onhashchange = onHashChange; }()); - -// This is required in firefox. Explanations: when going back in the history, firefox doesn't re-run -// the JS, therefore preventing rustdoc from setting a few things required to be able to reload the -// previous search results (if you navigated to a search result with the keyboard, pressed enter on -// it to navigate to that result, and then came back to this page). -window.onunload = function(){}; |
