FIX: Back button shenanigans when redirecting from `index` routes

FIX: Back button shenanigans when redirecting from index routes

This is a problem that has long plagued Discourse. The root issue here is that we have to implement our own link click handler, because Discourse allows users to create HTML blobs of content with links, and when those links are clicked they must be handled by the Ember router.

This always involved a certain amount of use of private Ember APIs which of course evolved over time.

The API has more or less stabilized in the last two years, however we have hacks in our URLs to handle a dynamic root path, depending on how forums have set up their filters and in what order.

This patch adds a special case for the root path so we needn’t update the URL ourselves otherwise, which preserves the back button on index routes. The update call would otherwise insert an extra history event if a route redirected on transition, breaking the back button.

diff --git a/app/assets/javascripts/discourse/lib/url.js.es6 b/app/assets/javascripts/discourse/lib/url.js.es6
index ec111f2..7a35499 100644
--- a/app/assets/javascripts/discourse/lib/url.js.es6
+++ b/app/assets/javascripts/discourse/lib/url.js.es6
@@ -433,8 +433,6 @@ const DiscourseURL = Ember.Object.extend({
       if (discoveryTopics) {
         discoveryTopics.resetParams();
       }
-
-      router._routerMicrolib.updateURL(path);
     }
 
     const split = path.split("#");
@@ -445,7 +443,16 @@ const DiscourseURL = Ember.Object.extend({
       elementId = split[1];
     }
 
-    const transition = router.handleURL(path);
+    // The default path has a hack to allow `/` to default to defaultHomepage
+    // via BareRouter.handleUrl
+    let transition;
+    if (path === "/") {
+      router._routerMicrolib.updateURL(path);
+      transition = router.handleURL(path);
+    } else {
+      transition = router.transitionTo(path);
+    }
+
     transition._discourse_intercepted = true;
     const promise = transition.promise || transition;
     promise.then(() => jumpToElement(elementId));
diff --git a/app/assets/javascripts/discourse/widgets/hamburger-menu.js.es6 b/app/assets/javascripts/discourse/widgets/hamburger-menu.js.es6
index 023128b..4b25906 100644
--- a/app/assets/javascripts/discourse/widgets/hamburger-menu.js.es6
+++ b/app/assets/javascripts/discourse/widgets/hamburger-menu.js.es6
@@ -59,7 +59,7 @@ export default createWidget("hamburger-menu", {
 
     if (currentUser.admin) {
       links.push({
-        href: "/admin/site_settings/category/required",
+        href: "/admin/site_settings",
         icon: "cog",
         label: "admin.site_settings.title",
         className: "settings-link"
diff --git a/app/assets/javascripts/discourse/widgets/user-menu.js.es6 b/app/assets/javascripts/discourse/widgets/user-menu.js.es6
index efd94ba..33e92f0 100644
--- a/app/assets/javascripts/discourse/widgets/user-menu.js.es6
+++ b/app/assets/javascripts/discourse/widgets/user-menu.js.es6
@@ -90,7 +90,7 @@ createWidget("user-menu-links", {
       label: "user.preferences",
       className: "user-preferences-link",
       icon: "cog",
-      href: `${path}/preferences/account`
+      href: `${path}/preferences`
     });
 
     return h("ul.menu-links-row", [

GitHub sha: 02f6db4e

1 Like

This commit has been mentioned on Discourse Meta. There might be relevant details there: