FIX: Ensure browser history contains correct URLs (#13367)

FIX: Ensure browser history contains correct URLs (#13367)

Clicking on an incomplete link to a topic (/t/ID or /t/SLUG) from another post could replace current history entry or create two: one for the incomplete URL and another one for the correct one. Going back was either impossible or took the user to a redirect loop, redirected back to /t/ID which redirected them again to /t/SLUG/ID.

diff --git a/app/assets/javascripts/discourse/app/routes/topic-by-slug-or-id.js b/app/assets/javascripts/discourse/app/routes/topic-by-slug-or-id.js
index 0cfe244..f2805ae 100644
--- a/app/assets/javascripts/discourse/app/routes/topic-by-slug-or-id.js
+++ b/app/assets/javascripts/discourse/app/routes/topic-by-slug-or-id.js
@@ -12,6 +12,17 @@ export default DiscourseRoute.extend({
   },
 
   afterModel(result) {
-    DiscourseURL.routeTo(result.url, { replaceURL: true });
+    // Using { replaceURL: true } to replace the current incomplete URL with
+    // the complete one is working incorrectly.
+    //
+    // Let's consider an example where the user is at /t/-/1. If they click on
+    // a link to /t/2 the expected behavior is to take the user to /t/2 that
+    // will redirect to /t/-/2 and generate a history with two entries: /t/-/1
+    // followed by /t/-/2.
+    //
+    // When { replaceURL: true } is present, the history contains a single
+    // entry /t/-/2. This suggests that `afterModel` is called in the context
+    // of the referrer replacing its entry with the new one.
+    DiscourseURL.routeTo(result.url);
   },
 });

GitHub sha: 27211ee7bbf661991cbf8a69f98d490be233ae26

This commit appears in #13367 which was approved by eviltrout. It was merged by udan11.