From 4d1bc3a491b0213679377d06cb2c03350ff6e767 Mon Sep 17 00:00:00 2001 From: Roman Rizzi Date: Wed, 16 Dec 2020 15:23:12 -0300 Subject: [PATCH] FIX: Scroll to the last position when navigating back to the tag topic list. (#11496) Scrolling was not working as expected after clicking the browser back button and navigating back to the tag topic list. We need to wrap the scroll inside a debounce function to ensure that the "window.pageYOffset" property is populated before our function runs. --- .../discourse/app/components/topic-list.js | 14 ++++++++++++-- .../discourse/app/templates/tags/show.hbs | 1 + 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/discourse/app/components/topic-list.js b/app/assets/javascripts/discourse/app/components/topic-list.js index 8c5919afe48..31f187628ae 100644 --- a/app/assets/javascripts/discourse/app/components/topic-list.js +++ b/app/assets/javascripts/discourse/app/components/topic-list.js @@ -1,9 +1,9 @@ import { alias, reads } from "@ember/object/computed"; +import { debounce, schedule } from "@ember/runloop"; import discourseComputed, { observes } from "discourse-common/utils/decorators"; import Component from "@ember/component"; import LoadMore from "discourse/mixins/load-more"; import { on } from "@ember/object/evented"; -import { schedule } from "@ember/runloop"; export default Component.extend(LoadMore, { tagName: "table", @@ -73,7 +73,17 @@ export default Component.extend(LoadMore, { let scrollTo = this.session.get("topicListScrollPosition"); if (scrollTo && scrollTo >= 0) { - schedule("afterRender", () => $(window).scrollTop(scrollTo + 1)); + schedule("afterRender", () => { + debounce( + this, + function () { + if (this.element && !this.isDestroying && !this.isDestroyed) { + $(window).scrollTop(scrollTo + 1); + } + }, + 0 + ); + }); } }, diff --git a/app/assets/javascripts/discourse/app/templates/tags/show.hbs b/app/assets/javascripts/discourse/app/templates/tags/show.hbs index a064771eb2b..38f50fab9b6 100644 --- a/app/assets/javascripts/discourse/app/templates/tags/show.hbs +++ b/app/assets/javascripts/discourse/app/templates/tags/show.hbs @@ -59,6 +59,7 @@ order=order ascending=ascending changeSort=(action "changeSort") + scrollOnLoad=true }} {{/discovery-topics-list}} {{/if}}