diff --git a/docs/helpers.md b/docs/helpers.md index 83c59784a..9cb511f96 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 c35179059..f3413a778 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 faf2e4f7c..71246bb15 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 d0b2a9a31..28fa2ea27 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' }); } });