FIX: Preserve anchors in permalink transitions (#10421)

When visiting a permalink with an anchor (e.g. /important-link#notes) the anchor part was being dropped during redirection.

The change doesn't have a test. Functions like scrollToPost or scrollToElement don't have any effect in the test environment.
This commit is contained in:
Jarek Radosz 2020-08-13 16:44:34 +02:00 committed by GitHub
parent 7a8442435c
commit ab5df7b2bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 1 deletions

View File

@ -480,6 +480,8 @@ const DiscourseURL = EmberObject.extend({
}
transition._discourse_intercepted = true;
transition._discourse_anchor = elementId;
const promise = transition.promise || transition;
promise.then(() => jumpToElement(elementId));
}

View File

@ -11,7 +11,18 @@ export default DiscourseRoute.extend({
if (results.found) {
// Avoid polluting the history stack for external links
transition.abort();
DiscourseURL.routeTo(results.target_url);
let url = results.target_url;
if (transition._discourse_anchor) {
// Remove the anchor from the permalink if present
url = url.split("#")[0];
// Add the anchor from the transition
url += `#${transition._discourse_anchor}`;
}
DiscourseURL.routeTo(url);
return "";
} else {
// 404 body HTML