Skip to content

Commit

Permalink
fix: cross-origin url cannot be redirected when "externalLinkTarget" …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
Koooooo-7 committed May 15, 2020
1 parent 6e554f8 commit fd2cec6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
8 changes: 8 additions & 0 deletions docs/helpers.md
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/core/config.js
Expand Up @@ -32,6 +32,7 @@ export default function() {
externalLinkRel: 'noopener',
routerMode: 'hash',
noCompileLinks: [],
crossOriginLinks: [],
relativePath: false,
topMargin: 0,
},
Expand Down
11 changes: 11 additions & 0 deletions src/core/render/compiler/link.js
Expand Up @@ -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)';
Expand Down
7 changes: 6 additions & 1 deletion src/core/router/history/html5.js
Expand Up @@ -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' });
}
});
Expand Down

0 comments on commit fd2cec6

Please sign in to comment.