From 6a389fd15a1d677daa47c090c06ad4ded47aec75 Mon Sep 17 00:00:00 2001 From: Jarek Radosz Date: Wed, 30 Nov 2022 16:55:30 +0100 Subject: [PATCH] FIX: ScrollingPostStream regressed in #15313 (#18584) regression commit: bec76f937cfb7564ff3c63c57e3372ba99378371 In case when the user navigates away between async actions that code would error out on a missing element. This adds a simple sanity check. --- .../app/components/scrolling-post-stream.js | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) 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 1b640951efc..901005e0866 100644 --- a/app/assets/javascripts/discourse/app/components/scrolling-post-stream.js +++ b/app/assets/javascripts/discourse/app/components/scrolling-post-stream.js @@ -190,20 +190,21 @@ export default MountWidget.extend({ refresh(() => { const refreshedElem = document.getElementById(elemId); - // Quickly going back might mean the element is destroyed - const position = domUtils.position(refreshedElem); - if (position && position.top) { - let whereY = position.top - distToElement; - document.documentElement.scroll({ top: whereY, left: 0 }); - - // 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. - schedule("afterRender", () => { - document.documentElement.scroll({ top: whereY, left: 0 }); - }); + if (!refreshedElem) { + return; } + + const position = domUtils.position(refreshedElem); + const top = position.top - distToElement; + document.documentElement.scroll({ top, left: 0 }); + + // 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. + schedule("afterRender", () => { + document.documentElement.scroll({ top, left: 0 }); + }); }); }; this.topVisibleChanged({