From 8871c9852c874070911ccb67c22e43a0c45ad91e Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Wed, 28 Sep 2016 05:02:15 -0400 Subject: [PATCH] FIX: Allow us to link to server side responses --- .../javascripts/discourse/lib/url.js.es6 | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/discourse/lib/url.js.es6 b/app/assets/javascripts/discourse/lib/url.js.es6 index 3391986f719..a37867135bc 100644 --- a/app/assets/javascripts/discourse/lib/url.js.es6 +++ b/app/assets/javascripts/discourse/lib/url.js.es6 @@ -5,6 +5,12 @@ import { defaultHomepage } from 'discourse/lib/utilities'; const rewrites = []; const TOPIC_REGEXP = /\/t\/([^\/]+)\/(\d+)\/?(\d+)?/; +// We can add links here that have server side responses but not client side. +const SERVER_SIDE_ONLY = [ + /^\/posts\/\d+\/raw/, + /^\/posts\.rss/ +]; + let _jumpScheduled = false; export function jumpToElement(elementId) { if (_jumpScheduled || Ember.isEmpty(elementId)) { return; } @@ -79,7 +85,7 @@ const DiscourseURL = Ember.Object.extend({ // Always use replaceState in the next runloop to prevent weird routes changing // while URLs are loading. For example, while a topic loads it sets `currentPost` // which triggers a replaceState even though the topic hasn't fully loaded yet! - Em.run.next(function() { + Ember.run.next(() => { const location = DiscourseURL.get('router.location'); if (location && location.replaceURL) { location.replaceURL(path); @@ -114,6 +120,15 @@ const DiscourseURL = Ember.Object.extend({ return; } + const serverSide = SERVER_SIDE_ONLY.some(r => { + if (path.match(r)) { + document.location = path; + return true; + } + }); + + if (serverSide) { return; } + // Protocol relative URLs if (path.indexOf('//') === 0) { document.location = path; @@ -151,6 +166,7 @@ const DiscourseURL = Ember.Object.extend({ rewrites.forEach(rw => path = path.replace(rw.regexp, rw.replacement)); if (this.navigatedToPost(oldPath, path, opts)) { return; } + // Schedule a DOM cleanup event Em.run.scheduleOnce('afterRender', Discourse.Route, 'cleanDOM'); @@ -256,7 +272,7 @@ const DiscourseURL = Ember.Object.extend({ @param {String} oldPath the previous path we were on @param {String} path the path we're navigating to **/ - navigatedToHome: function(oldPath, path) { + navigatedToHome(oldPath, path) { const homepage = defaultHomepage(); if (window.history && @@ -271,7 +287,7 @@ const DiscourseURL = Ember.Object.extend({ }, // This has been extracted so it can be tested. - origin: function() { + origin() { return window.location.origin + (Discourse.BaseUri === "/" ? '' : Discourse.BaseUri); },