Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix #1046] fix cross-origin url cannot be redirected when "externalLinkTarget" is set to "_self" and "routerMode" is set to "history". #1062

Merged
merged 5 commits into from
May 15, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ 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 set the `routerMode: 'history'` and `externalLinkTarget: '_self'` , you need add this configuration for those Cross-Origin links
Koooooo-7 marked this conversation as resolved.
Show resolved Hide resolved
```md
[example.com](https://example.com/ ':crossorgin')
```

## Github Task Lists

```md
Expand Down
1 change: 1 addition & 0 deletions src/core/config.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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