From 57fe57e7ce0a2a4c5ac3ce54184473b32aa33fab Mon Sep 17 00:00:00 2001 From: Penar Musaraj Date: Tue, 6 Dec 2022 09:52:59 -0500 Subject: [PATCH] FIX: URL fragments not purging (#19324) Fixes issue introduced in commit 2704a02e3a23401f8225a3bd1ceebc528ee43919. Once a user visited an in-page anchor, that URL "stuck" in the browser's address bar. See also https://meta.discourse.org/t/anchors-url-not-purge-when-page-changed/244484 for a detailed report. --- app/assets/javascripts/discourse/app/lib/url.js | 2 +- app/assets/javascripts/discourse/tests/unit/lib/url-test.js | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/discourse/app/lib/url.js b/app/assets/javascripts/discourse/app/lib/url.js index 0bd9a28efbf..996a51f94dd 100644 --- a/app/assets/javascripts/discourse/app/lib/url.js +++ b/app/assets/javascripts/discourse/app/lib/url.js @@ -216,7 +216,7 @@ const DiscourseURL = EmberObject.extend({ const m = /^#(.+)$/.exec(path); if (m) { this.jumpToElement(m[1]); - return; + return this.replaceState(path); } const oldPath = this.router.currentURL; diff --git a/app/assets/javascripts/discourse/tests/unit/lib/url-test.js b/app/assets/javascripts/discourse/tests/unit/lib/url-test.js index bdb99b3ce48..1d1cc04dc89 100644 --- a/app/assets/javascripts/discourse/tests/unit/lib/url-test.js +++ b/app/assets/javascripts/discourse/tests/unit/lib/url-test.js @@ -154,10 +154,15 @@ module("Unit | Utility | url", function () { test("anchor handling", async function (assert) { sinon.stub(DiscourseURL, "jumpToElement"); + sinon.stub(DiscourseURL, "replaceState"); DiscourseURL.routeTo("#heading1"); assert.ok( DiscourseURL.jumpToElement.calledWith("heading1"), "in-page anchors call jumpToElement" ); + assert.ok( + DiscourseURL.replaceState.calledWith("#heading1"), + "in-page anchors call replaceState with the url fragment" + ); }); });