Skip to content

A platform for community discussion. Free, open, simple.

FIX: Initial scroll position for nested replies topics when not cached (#40337)

Mark VanLandinghamd23be65e17ee
diff --git a/frontend/discourse/app/routes/nested.js b/frontend/discourse/app/routes/nested.js
index b8dc4080765..35ee492425f 100644
--- a/frontend/discourse/app/routes/nested.js
+++ b/frontend/discourse/app/routes/nested.js
@@ -1,5 +1,6 @@
import { getOwner } from "@ember/owner";
import Route from "@ember/routing/route";
+import { schedule } from "@ember/runloop";
import { service } from "@ember/service";
import { isEmpty } from "@ember/utils";
import { ajax } from "discourse/lib/ajax";
@@ -68,11 +69,12 @@ export default class NestedRoute extends Route {
}
setupController(controller, model) {
- if (this._restoringFromCache) {
- controller.expansionState = this._restoringFromCache.expansionState;
- controller.fetchedChildrenCache =
- this._restoringFromCache.fetchedChildrenCache;
- controller.scrollAnchor = this._restoringFromCache.scrollAnchor;
+ const restoringFromCache = this._restoringFromCache;
+
+ if (restoringFromCache) {
+ controller.expansionState = restoringFromCache.expansionState;
+ controller.fetchedChildrenCache = restoringFromCache.fetchedChildrenCache;
+ controller.scrollAnchor = restoringFromCache.scrollAnchor;
this._restoringFromCache = null;
} else {
controller.expansionState = new Map();
@@ -115,6 +117,12 @@ export default class NestedRoute extends Route {
topic: model.topic,
});
}
+
+ if (!restoringFromCache && !model.contextMode) {
+ // Nested opts out of the global scroll manager for cache restoration,
+ // so fresh root-topic entries need their own top reset.
+ schedule("afterRender", () => window.scrollTo(0, 0));
+ }
}
deactivate() {
diff --git a/spec/system/nested_category_default_spec.rb b/spec/system/nested_category_default_spec.rb
index 92a6dd747e6..688a409afc6 100644
--- a/spec/system/nested_category_default_spec.rb
+++ b/spec/system/nested_category_default_spec.rb
@@ -15,6 +15,7 @@ RSpec.describe "Nested view category default" do
let(:nested_view) { PageObjects::Pages::NestedView.new }
let(:category_page) { PageObjects::Pages::Category.new }
+ let(:topic_list) { PageObjects::Components::TopicList.new }
let(:form) { PageObjects::Components::FormKit.new(".form-kit") }
before do
@@ -63,6 +64,20 @@ RSpec.describe "Nested view category default" do
expect(nested_view).to have_nested_view
end
+ it "takes the user to the top of a nested topic from a scrolled list" do
+ category_page.visit(nested_category)
+ expect(topic_list).to have_topic(topic)
+
+ topic_list.scroll_page_down
+ expect(topic_list).to be_scrolled_down
+
+ topic_list.visit_topic(topic)
+
+ expect(page).to have_current_path(%r{/n/#{topic.slug}/#{topic.id}})
+ expect(nested_view).to have_nested_view
+ try_until_success { expect(page.evaluate_script("window.scrollY")).to eq(0) }
+ end
+
it "does not redirect topics in categories without nested default" do
normal_topic = Fabricate(:topic, user: user, category: category)
Fabricate(:post, topic: normal_topic, user: user, post_number: 1)
diff --git a/spec/system/page_objects/components/topic_list.rb b/spec/system/page_objects/components/topic_list.rb
index e5ec15a479f..90a7266fb87 100644
--- a/spec/system/page_objects/components/topic_list.rb
+++ b/spec/system/page_objects/components/topic_list.rb
@@ -90,6 +90,22 @@ module PageObjects
find("#{topic_list_item_class(topic)} a.raw-topic-link").click
end
+ def scroll_page_down(amount: 50)
+ page.execute_script(<<~JS, amount)
+ const listArea = document.querySelector("#list-area");
+ if (!listArea) {
+ throw new Error("Topic list area not found");
+ }
+
+ listArea.style.minHeight = "3000px";
+ window.scrollTo(0, arguments[0]);
+ JS
+ end
+
+ def scrolled_down?
+ page.evaluate_script("window.scrollY").positive?
+ end
+
def visit_topic_last_reply_via_keyboard(topic)
find("#{topic_list_item_class(topic)} a.post-activity").send_keys(:return)
end