Skip to content

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

FIX: Keep the anchor in the URL when routing to another topic (#44498)

Renato Atiliob8e717012a82
diff --git a/frontend/discourse/app/lib/url.js b/frontend/discourse/app/lib/url.js
index 524c1c86662..d0e90e7a5fa 100644
--- a/frontend/discourse/app/lib/url.js
+++ b/frontend/discourse/app/lib/url.js
@@ -533,8 +533,13 @@ class DiscourseURL extends EmberObject {
transition._discourse_anchor = elementId;
transition._discourse_original_url = path;
- const promise = transition.promise || transition;
- return promise.then(() => this.jumpToElement(elementId));
+ const promise = transition.followRedirects?.() || transition;
+ return promise.then(() => {
+ if (elementId) {
+ this.replaceState(`#${elementId}`);
+ }
+ this.jumpToElement(elementId);
+ });
}
jumpToElement(elementId) {
diff --git a/frontend/discourse/tests/unit/lib/url-test.js b/frontend/discourse/tests/unit/lib/url-test.js
index e45526c9088..30190a5618d 100644
--- a/frontend/discourse/tests/unit/lib/url-test.js
+++ b/frontend/discourse/tests/unit/lib/url-test.js
@@ -451,6 +451,43 @@ module("Unit | Utility | url", function (hooks) {
);
});
+ test("handleURL restores the anchor after the transition", async function (assert) {
+ let redirected = false;
+ sinon.stub(DiscourseURL, "routerService").get(() => ({
+ transitionTo: () => ({
+ promise: Promise.resolve(),
+ followRedirects: () =>
+ new Promise((resolve) =>
+ setTimeout(() => {
+ redirected = true;
+ resolve();
+ }, 0)
+ ),
+ }),
+ }));
+ sinon.stub(DiscourseURL, "jumpToElement");
+ sinon.stub(DiscourseURL, "replaceState");
+
+ await DiscourseURL.handleURL("/t/some-topic/123#heading1");
+
+ assert.true(redirected, "waits for redirects before restoring");
+ assert.true(
+ DiscourseURL.replaceState.calledWith("#heading1"),
+ "the anchor is put back on the URL once the route has settled"
+ );
+ assert.true(
+ DiscourseURL.jumpToElement.calledWith("heading1"),
+ "the anchor is scrolled to"
+ );
+
+ DiscourseURL.replaceState.resetHistory();
+ await DiscourseURL.handleURL("/t/some-topic/123");
+ assert.false(
+ DiscourseURL.replaceState.called,
+ "no replaceState without an anchor"
+ );
+ });
+
test("getCanonicalUrl", function (assert) {
assert.strictEqual(
getCanonicalUrl("http://eviltrout.com/t/this-is-a-test/1/"),
diff --git a/spec/system/topic_page_spec.rb b/spec/system/topic_page_spec.rb
index 73822fa9334..52444378c07 100644
--- a/spec/system/topic_page_spec.rb
+++ b/spec/system/topic_page_spec.rb
@@ -20,6 +20,48 @@ describe "Topic page" do
expect(page).to have_current_path(%r{/t/#{topic.slug}/#{topic.id}#toc-h2-testing}, url: true)
end
+ context "with links to an anchor in another topic" do
+ fab!(:source_topic, :topic)
+
+ before do
+ filler = (1..40).map { |i| "<p>Filler paragraph #{i}</p>" }.join
+ topic.first_post.update!(cooked: <<~HTML)
+ #{filler}
+ <a name="deep-anchor"></a>
+ <h2 id="deep-heading">Deep heading</h2>
+ HTML
+ Fabricate(:post, topic: source_topic, cooked: <<~HTML)
+ <p><a class="slugged-link" href="/t/#{topic.slug}/#{topic.id}#deep-anchor">slugged</a></p>
+ <p><a class="slugless-link" href="/t/#{topic.id}#deep-anchor">slugless</a></p>
+ HTML
+ end
+
+ def expect_anchor_in_view
+ expect(page).to have_current_path(%r{/t/#{topic.slug}/#{topic.id}#deep-anchor}, url: true)
+ expect(page).to have_css("#deep-heading")
+ top =
+ page.evaluate_script("document.querySelector('#deep-heading').getBoundingClientRect().top")
+ height = page.evaluate_script("window.innerHeight")
+ expect(top).to be_between(0, height)
+ end
+
+ it "keeps the anchor in the URL and scrolls to it" do
+ visit("/t/#{source_topic.slug}/#{source_topic.id}")
+
+ find("a.slugged-link").click
+
+ expect_anchor_in_view
+ end
+
+ it "keeps the anchor through the slugless topic redirect" do
+ visit("/t/#{source_topic.slug}/#{source_topic.id}")
+
+ find("a.slugless-link").click
+
+ expect_anchor_in_view
+ end
+ end
+
context "with a subfolder setup" do
before { set_subfolder "/forum" }