diff --git a/lib/link.js b/lib/link.js index eca2e798b1670d6c516a84aea17fbe48d32ba532..873fe936e34c1acdb4df3b4e822d1998224c9ffb 100644 --- a/lib/link.js +++ b/lib/link.js @@ -1,7 +1,7 @@ import { resolve } from 'url' import React, { Component, Children, PropTypes } from 'react' import Router from './router' -import { warn, execOnce } from './utils' +import { warn, execOnce, getLocationOrigin } from './utils' export default class Link extends Component { constructor (props) { @@ -84,7 +84,7 @@ export default class Link extends Component { } export function isLocal (href) { - const origin = window.location.origin + const origin = getLocationOrigin() return !/^(https?:)?\/\//.test(href) || origin === href.substr(0, origin.length) } diff --git a/lib/router/router.js b/lib/router/router.js index 9f16572378133ab6c521b266dc27210025308963..dc1cd919e53064e48b390e39e73b869dc51786b4 100644 --- a/lib/router/router.js +++ b/lib/router/router.js @@ -5,7 +5,7 @@ import evalScript from '../eval-script' import shallowEquals from '../shallow-equals' import { EventEmitter } from 'events' import { reloadIfPrefetched } from '../prefetch' -import { loadGetInitialProps } from '../utils' +import { loadGetInitialProps, getLocationOrigin } from '../utils' export default class Router extends EventEmitter { constructor (pathname, query, { Component, ErrorComponent, err } = {}) { @@ -283,7 +283,8 @@ export default class Router extends EventEmitter { } function getURL () { - const { href, origin } = window.location + const { href } = window.location + const origin = getLocationOrigin() return href.substring(origin.length) } diff --git a/lib/utils.js b/lib/utils.js index b1757f98784963af1910515701d5e485b6d126b5..d5ba6c5f301b2b17b3cfa8ff7ecde6ee9d1129ad 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -53,3 +53,8 @@ export async function loadGetInitialProps (Component, ctx) { } return props } + +export function getLocationOrigin () { + const { protocol, hostname, port } = window.location + return `${protocol}//${hostname}${port ? ':' + port : ''}` +}