From d382e4db9a16db9627bffa0e6dc0944fe3cf46f7 Mon Sep 17 00:00:00 2001 From: Eric Zingeler Date: Wed, 15 Feb 2017 00:31:19 -0800 Subject: [PATCH] replace location.origin usage with new utils/getLocationOrigin (#1142) --- lib/link.js | 4 ++-- lib/router/router.js | 5 +++-- lib/utils.js | 5 +++++ 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/link.js b/lib/link.js index eca2e798..873fe936 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 9f165723..dc1cd919 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 b1757f98..d5ba6c5f 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 : ''}` +}