FIX: Restore stream position in safari (#9993)

FIX: Restore stream position in safari (#9993)

Safari uses an aggressive back/forward cache, which means the app loads very quickly when hitting Back. But, in topics with > 30 posts, hitting Back runs post stream calculations too early, which means that users get taken back to an earlier point in the stream, consistently.

Using onpageshow, we can restore the correct location before the post stream calculations take place.

diff --git a/app/assets/javascripts/discourse/app/components/scrolling-post-stream.js b/app/assets/javascripts/discourse/app/components/scrolling-post-stream.js
index cb9b3da..399e2a7 100644
--- a/app/assets/javascripts/discourse/app/components/scrolling-post-stream.js
+++ b/app/assets/javascripts/discourse/app/components/scrolling-post-stream.js
@@ -333,6 +333,13 @@ export default MountWidget.extend({
     });
 
     this.appEvents.on("post-stream:refresh", this, "_refresh");
+
+    // restore scroll position on browsers with aggressive BFCaches (like Safari)
+    window.onpageshow = function(event) {
+      if (event.persisted) {
+        DiscourseURL.routeTo(this.location.pathname);
+      }
+    };
   },
 
   willDestroyElement() {

GitHub sha: bdba17cd

1 Like

This commit appears in #9993 which was merged by pmusaraj.