From fd2cec6bd66c46d6957811fefae4c615c3052a4f Mon Sep 17 00:00:00 2001 From: Koy <369491420@qq.com> Date: Fri, 15 May 2020 13:30:36 +0800 Subject: [PATCH] fix: cross-origin url cannot be redirected when "externalLinkTarget" is set to "_self" and "routerMode" is set to "history". (#1062) * [fix #1046] fix cross-origin url cannot be redirected when "externalLinkTarget" is set to "_self" and "routerMode" is set to "history". * [code format] code format. * update docs * docs refine. * fix(core): cross-orgin link work incorrect (#1046) Fix cross-origin url cannot be redirected when "externalLinkTarget" is set to "_self" and "routerMode" is set to "history". Add new configuration for those cases and completed docs. Fixes #1046 PR Close #1062 --- docs/helpers.md | 8 ++++++++ src/core/config.js | 1 + src/core/render/compiler/link.js | 11 +++++++++++ src/core/router/history/html5.js | 7 ++++++- 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/docs/helpers.md b/docs/helpers.md index 83c59784..9cb511f9 100644 --- a/docs/helpers.md +++ b/docs/helpers.md @@ -65,6 +65,14 @@ You will get `link`html. Do not worry, you can still set ti [link](/demo ':disabled') ``` +## Cross-Origin link + +Only when you both set the `routerMode: 'history'` and `externalLinkTarget: '_self'`, you need add this configuration for those Cross-Origin links. + +```md +[example.com](https://example.com/ ':crossorgin') +``` + ## Github Task Lists ```md diff --git a/src/core/config.js b/src/core/config.js index c3517905..f3413a77 100644 --- a/src/core/config.js +++ b/src/core/config.js @@ -32,6 +32,7 @@ export default function() { externalLinkRel: 'noopener', routerMode: 'hash', noCompileLinks: [], + crossOriginLinks: [], relativePath: false, topMargin: 0, }, diff --git a/src/core/render/compiler/link.js b/src/core/render/compiler/link.js index faf2e4f7..71246bb1 100644 --- a/src/core/render/compiler/link.js +++ b/src/core/render/compiler/link.js @@ -30,6 +30,17 @@ export const linkCompiler = ({ renderer, router, linkTarget, compilerClass }) => attrs.push(`target="${config.target}"`); } + // special case to check crossorigin urls + if ( + config.crossorgin && + linkTarget === '_self' && + compilerClass.config.routerMode === 'history' + ) { + if (compilerClass.config.crossOriginLinks.indexOf(href) === -1) { + compilerClass.config.crossOriginLinks.push(href); + } + } + if (config.disabled) { attrs.push('disabled'); href = 'javascript:void(0)'; diff --git a/src/core/router/history/html5.js b/src/core/router/history/html5.js index d0b2a9a3..28fa2ea2 100644 --- a/src/core/router/history/html5.js +++ b/src/core/router/history/html5.js @@ -27,7 +27,12 @@ export class HTML5History extends History { if (el.tagName === 'A' && !/_blank/.test(el.target)) { e.preventDefault(); const url = el.href; - window.history.pushState({ key: url }, '', url); + // solve history.pushState cross-origin issue + if (this.config.crossOriginLinks.indexOf(url) !== -1) { + window.open(url, '_self'); + } else { + window.history.pushState({ key: url }, '', url); + } cb({ event: e, source: 'navigate' }); } });