Skip to content

Commit fd2cec6

Browse files
authored
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
1 parent 6e554f8 commit fd2cec6

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

Diff for: docs/helpers.md

+8
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ You will get `<a href="/demo/">link</a>`html. Do not worry, you can still set ti
6565
[link](/demo ':disabled')
6666
```
6767

68+
## Cross-Origin link
69+
70+
Only when you both set the `routerMode: 'history'` and `externalLinkTarget: '_self'`, you need add this configuration for those Cross-Origin links.
71+
72+
```md
73+
[example.com](https://example.com/ ':crossorgin')
74+
```
75+
6876
## Github Task Lists
6977

7078
```md

Diff for: src/core/config.js

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export default function() {
3232
externalLinkRel: 'noopener',
3333
routerMode: 'hash',
3434
noCompileLinks: [],
35+
crossOriginLinks: [],
3536
relativePath: false,
3637
topMargin: 0,
3738
},

Diff for: src/core/render/compiler/link.js

+11
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@ export const linkCompiler = ({ renderer, router, linkTarget, compilerClass }) =>
3030
attrs.push(`target="${config.target}"`);
3131
}
3232

33+
// special case to check crossorigin urls
34+
if (
35+
config.crossorgin &&
36+
linkTarget === '_self' &&
37+
compilerClass.config.routerMode === 'history'
38+
) {
39+
if (compilerClass.config.crossOriginLinks.indexOf(href) === -1) {
40+
compilerClass.config.crossOriginLinks.push(href);
41+
}
42+
}
43+
3344
if (config.disabled) {
3445
attrs.push('disabled');
3546
href = 'javascript:void(0)';

Diff for: src/core/router/history/html5.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ export class HTML5History extends History {
2727
if (el.tagName === 'A' && !/_blank/.test(el.target)) {
2828
e.preventDefault();
2929
const url = el.href;
30-
window.history.pushState({ key: url }, '', url);
30+
// solve history.pushState cross-origin issue
31+
if (this.config.crossOriginLinks.indexOf(url) !== -1) {
32+
window.open(url, '_self');
33+
} else {
34+
window.history.pushState({ key: url }, '', url);
35+
}
3136
cb({ event: e, source: 'navigate' });
3237
}
3338
});

0 commit comments

Comments
 (0)