FIX: Upwards scrolling occasionally broken

FIX: Upwards scrolling occasionally broken

This fixes a bug in Chrome where upwards scrolling would occasionally
not work properly.

From 8bca2647eae891c2ec89720b08eb0737fd43a87c Mon Sep 17 00:00:00 2001
From: Robin Ward <robin.ward@gmail.com>
Date: Mon, 26 Nov 2018 13:36:44 -0500
Subject: [PATCH] FIX: Upwards scrolling occasionally broken

This fixes a bug in Chrome where upwards scrolling would occasionally
not work properly.

diff --git a/app/assets/javascripts/discourse/components/scrolling-post-stream.js.es6 b/app/assets/javascripts/discourse/components/scrolling-post-stream.js.es6
index faf0c66..b62b440 100644
--- a/app/assets/javascripts/discourse/components/scrolling-post-stream.js.es6
+++ b/app/assets/javascripts/discourse/components/scrolling-post-stream.js.es6
@@ -192,7 +192,14 @@ export default MountWidget.extend({
             // Quickly going back might mean the element is destroyed
             const position = $refreshedElem.position();
             if (position && position.top) {
-              $("html, body").scrollTop(position.top + distToElement);
+              let whereY = position.top + distToElement;
+              $("html, body").scrollTop(whereY);
+
+              // This seems weird, but somewhat infrequently a rerender
+              // will cause the browser to scroll to the top of the document
+              // in Chrome. This makes sure the scroll works correctly if that
+              // happens.
+              Ember.run.next(() => $("html, body").scrollTop(whereY));
             }
           });
         };

GitHub

3 Likes

hmmm I wonder if this is easily reproducible on local and still an issue with latest ember? I assume it will not be resolved by our upgrade.

1 Like

It’s not easily reproduced, but you can open a topic with 60 or so posts at the bottom and scroll up. About 30-50% of the time you’ll see the bug.

Honestly I suspect this is not Ember related as it happens with our virtual dom stuff. I got the browser to do a debugger on it and it had scrolled to the top, rendering nothing. It feels like a Chrome bug to me.

2 Likes