From dc6524200d587d89e6da375544a5fc08483178d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Weber?= Date: Sat, 16 Mar 2024 01:14:42 +0100 Subject: [PATCH] nav: browser back navigation does not jump to the correct position #509 --- .../content/basics/migration/_index.en.md | 2 ++ static/js/theme.js | 24 +++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/exampleSite/content/basics/migration/_index.en.md b/exampleSite/content/basics/migration/_index.en.md index 154e40f8ae..775dafc7cf 100644 --- a/exampleSite/content/basics/migration/_index.en.md +++ b/exampleSite/content/basics/migration/_index.en.md @@ -26,6 +26,8 @@ This document shows you what's new in the latest release and flags it with one o - {{% badge style="info" icon="plus-circle" title=" " %}}New{{% /badge %}} The theme has updated its Mermaid dependency to 10.6.0. This adds support for [block diagrams](shortcodes/mermaid#block-diagram). +- {{% badge style="info" icon="plus-circle" title=" " %}}New{{% /badge %}} This release fixes a long outstanding bug where the page wasn't repositioning correctly when going forward or backward in your browser history. + --- ## 5.25.0 (2024-02-29) {#5250} diff --git a/static/js/theme.js b/static/js/theme.js index 7356e81e4b..d1e293da98 100644 --- a/static/js/theme.js +++ b/static/js/theme.js @@ -1167,7 +1167,19 @@ function initScrollPositionSaver(){ state.contentScrollTop = +elc.scrollTop; window.history.replaceState( state, '', window.location ); }; - window.addEventListener( 'pagehide', savePosition ); + + var ticking = false; + elc.addEventListener( 'scroll', function( event ){ + if( !ticking ){ + window.requestAnimationFrame( function(){ + savePosition(); + ticking = false; + }); + ticking = true; + } + }); + + document.addEventListener( "click", savePosition ); } function scrollToPositions() { @@ -1228,6 +1240,15 @@ function scrollToPositions() { } } +window.addEventListener( 'popstate', function ( event ){ + scrollToPositions(); +}); + +const observer = new PerformanceObserver( function(){ + scrollToPositions(); +}); +observer.observe({ type: "navigation" }); + function mark() { // mark some additional stuff as searchable var bodyInnerLinks = document.querySelectorAll( '#R-body-inner a:not(.lightbox-link):not(.btn):not(.lightbox-back)' ); @@ -1525,7 +1546,6 @@ ready( function(){ initImage(); initExpand(); initScrollPositionSaver(); - scrollToPositions(); }); (function(){