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
This commit is contained in:
Koy 2020-05-15 13:30:36 +08:00 committed by GitHub
parent 6e554f8ebd
commit fd2cec6bd6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 1 deletions

View File

@ -65,6 +65,14 @@ You will get `<a href="/demo/">link</a>`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

View File

@ -32,6 +32,7 @@ export default function() {
externalLinkRel: 'noopener',
routerMode: 'hash',
noCompileLinks: [],
crossOriginLinks: [],
relativePath: false,
topMargin: 0,
},

View File

@ -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)';

View File

@ -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' });
}
});