Commit d382e4db authored by Eric Zingeler's avatar Eric Zingeler Committed by Arunoda Susiripala

replace location.origin usage with new utils/getLocationOrigin (#1142)

parent 437fb2cb
import { resolve } from 'url' import { resolve } from 'url'
import React, { Component, Children, PropTypes } from 'react' import React, { Component, Children, PropTypes } from 'react'
import Router from './router' import Router from './router'
import { warn, execOnce } from './utils' import { warn, execOnce, getLocationOrigin } from './utils'
export default class Link extends Component { export default class Link extends Component {
constructor (props) { constructor (props) {
...@@ -84,7 +84,7 @@ export default class Link extends Component { ...@@ -84,7 +84,7 @@ export default class Link extends Component {
} }
export function isLocal (href) { export function isLocal (href) {
const origin = window.location.origin const origin = getLocationOrigin()
return !/^(https?:)?\/\//.test(href) || return !/^(https?:)?\/\//.test(href) ||
origin === href.substr(0, origin.length) origin === href.substr(0, origin.length)
} }
......
...@@ -5,7 +5,7 @@ import evalScript from '../eval-script' ...@@ -5,7 +5,7 @@ import evalScript from '../eval-script'
import shallowEquals from '../shallow-equals' import shallowEquals from '../shallow-equals'
import { EventEmitter } from 'events' import { EventEmitter } from 'events'
import { reloadIfPrefetched } from '../prefetch' import { reloadIfPrefetched } from '../prefetch'
import { loadGetInitialProps } from '../utils' import { loadGetInitialProps, getLocationOrigin } from '../utils'
export default class Router extends EventEmitter { export default class Router extends EventEmitter {
constructor (pathname, query, { Component, ErrorComponent, err } = {}) { constructor (pathname, query, { Component, ErrorComponent, err } = {}) {
...@@ -283,7 +283,8 @@ export default class Router extends EventEmitter { ...@@ -283,7 +283,8 @@ export default class Router extends EventEmitter {
} }
function getURL () { function getURL () {
const { href, origin } = window.location const { href } = window.location
const origin = getLocationOrigin()
return href.substring(origin.length) return href.substring(origin.length)
} }
......
...@@ -53,3 +53,8 @@ export async function loadGetInitialProps (Component, ctx) { ...@@ -53,3 +53,8 @@ export async function loadGetInitialProps (Component, ctx) {
} }
return props return props
} }
export function getLocationOrigin () {
const { protocol, hostname, port } = window.location
return `${protocol}//${hostname}${port ? ':' + port : ''}`
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment