FIX: Topic queryParams are removed from history state when scrolling. (#14881)

FIX: Topic queryParams are removed from history state when scrolling. (#14881)

  • Also fixed a bug where the queryParams are not removed when toggling between filters.
diff --git a/app/assets/javascripts/discourse/app/controllers/topic.js b/app/assets/javascripts/discourse/app/controllers/topic.js
index 2382a26..c5870ca 100644
--- a/app/assets/javascripts/discourse/app/controllers/topic.js
+++ b/app/assets/javascripts/discourse/app/controllers/topic.js
@@ -103,7 +103,17 @@ export default Controller.extend(bufferedProperty("model"), {
   ),
 
   updateQueryParams() {
-    this.setProperties(this.get("model.postStream.streamFilters"));
+    const filters = this.get("model.postStream.streamFilters");
+
+    if (Object.keys(filters).length > 0) {
+      this.setProperties(filters);
+    } else {
+      this.setProperties({
+        username_filters: null,
+        filter: null,
+        replies_to_post_number: null,
+      });
+    }
   },
 
   @observes("model.title", "category")
diff --git a/app/assets/javascripts/discourse/app/routes/topic.js b/app/assets/javascripts/discourse/app/routes/topic.js
index ae99745..a5c26b1 100644
--- a/app/assets/javascripts/discourse/app/routes/topic.js
+++ b/app/assets/javascripts/discourse/app/routes/topic.js
@@ -183,10 +183,32 @@ const TopicRoute = DiscourseRoute.extend({
       }
 
       const topic = this.modelFor("topic");
+
       if (topic && currentPost) {
-        let postUrl = topic.get("url");
+        let postUrl;
+
         if (currentPost > 1) {
-          postUrl += "/" + currentPost;
+          postUrl = topic.urlForPostNumber(currentPost);
+        } else {
+          postUrl = topic.url;
+        }
+
+        if (this._router.currentRoute.queryParams) {
+          let searchParams;
+
+          Object.entries(this._router.currentRoute.queryParams).map(
+            ([key, value]) => {
+              if (!searchParams) {
+                searchParams = new URLSearchParams();
+              }
+
+              searchParams.append(key, value);
+            }
+          );
+
+          if (searchParams) {
+            postUrl += `?${searchParams.toString()}`;
+          }
         }
 
         cancel(this.scheduledReplace);

GitHub sha: 095255c8ec2a922b6cf30111c28c0d4224507bc5

This commit appears in #14881 which was approved by martin. It was merged by tgxworld.