Skip to content

Commit

Permalink
redo
Browse files Browse the repository at this point in the history
  • Loading branch information
Janpot committed Jul 31, 2020
1 parent 15bcf71 commit dae4367
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions packages/next/next-server/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,16 +265,17 @@ export default class Router implements BaseRouter {
this.isFallback = isFallback

if (typeof window !== 'undefined') {
const browserUrl = getURL()
// make sure "as" doesn't start with double slashes or else it can
// throw an error as it's considered invalid
if (as.substr(0, 2) !== '//') {
if (as.substr(0, 2) !== '//' && browserUrl.startsWith(basePath)) {
// in order for `e.state` to work on the `onpopstate` event
// we have to register the initial route upon initialization

// if it doesn't start with the basePath, it's to be treated as an external url
this.changeState(
'replaceState',
formatWithValidation({ pathname: addBasePath(pathname), query }),
getURL()
formatWithValidation({ pathname, query }),
delBasePath(browserUrl)
)
}

Expand Down Expand Up @@ -333,12 +334,16 @@ export default class Router implements BaseRouter {
// But we can simply replace the state with the new changes.
// Actually, for (1) we don't need to nothing. But it's hard to detect that event.
// So, doing the following for (1) does no harm.
const { pathname, query } = this
this.changeState(
'replaceState',
formatWithValidation({ pathname: addBasePath(pathname), query }),
getURL()
)
const browserUrl = getURL()
if (browserUrl.startsWith(basePath)) {
// if it doesn't start with the basePath, it's to be treated as an external url
const { pathname, query } = this
this.changeState(
'replaceState',
formatWithValidation({ pathname, query }),
delBasePath(browserUrl)
)
}
return
}

Expand Down Expand Up @@ -368,7 +373,7 @@ export default class Router implements BaseRouter {
)
}
}
this.change('replaceState', delBasePath(url), delBasePath(as), options)
this.change('replaceState', url, as, options)
}

update(route: string, mod: any) {
Expand Down Expand Up @@ -434,7 +439,6 @@ export default class Router implements BaseRouter {
options: any
): Promise<boolean> {
let { url, as } = prepareUrlAs(this.pathname, urlIn, asIn)
const browserUrl = addBasePath(url)
let browserAs = addBasePath(as)

if (!options._h) {
Expand Down Expand Up @@ -471,7 +475,7 @@ export default class Router implements BaseRouter {
if (!options._h && this.onlyAHashChange(as)) {
this.asPath = as
Router.events.emit('hashChangeStart', browserAs)
this.changeState(method, browserUrl, browserAs, options)
this.changeState(method, url, as, options)
this.scrollToHash(as)
Router.events.emit('hashChangeComplete', browserAs)
return true
Expand Down Expand Up @@ -539,7 +543,7 @@ export default class Router implements BaseRouter {
const { error } = routeInfo

Router.events.emit('beforeHistoryChange', browserAs)
this.changeState(method, browserUrl, browserAs, options)
this.changeState(method, url, as, options)

if (process.env.NODE_ENV !== 'production') {
const appComp: any = this.components['/_app'].Component
Expand Down Expand Up @@ -589,7 +593,8 @@ export default class Router implements BaseRouter {
}
}

if (method !== 'pushState' || getURL() !== as) {
const browserAs = addBasePath(as)
if (method !== 'pushState' || getURL() !== browserAs) {
window.history[method](
{
url,
Expand All @@ -601,7 +606,7 @@ export default class Router implements BaseRouter {
// Passing the empty string here should be safe against future changes to the method.
// https://developer.mozilla.org/en-US/docs/Web/API/History/replaceState
'',
as
browserAs
)
}
}
Expand Down

0 comments on commit dae4367

Please sign in to comment.