diff --git a/packages/next-server/lib/router/router.js b/packages/next-server/lib/router/router.js index d4f4c1d592f6b..cca8873841b07 100644 --- a/packages/next-server/lib/router/router.js +++ b/packages/next-server/lib/router/router.js @@ -188,14 +188,14 @@ export default class Router { return true } - const { pathname: asPathname, query: asQuery } = parse(as, true) const { pathname, query } = parse(url, true) // If asked to change the current URL we should reload the current page // (not location.reload() but reload getInitialProps and other Next.js stuffs) // We also need to set the method = replaceState always // as this should not go into the history (That's how browsers work) - if (!this.urlIsNew(asPathname, asQuery)) { + // We should compare the new asPath to the current asPath, not the url + if (!this.urlIsNew(as)) { method = 'replaceState' } @@ -366,8 +366,10 @@ export default class Router { } } - urlIsNew (pathname, query) { - return this.pathname !== pathname || !shallowEquals(query, this.query) + urlIsNew (asPath) { + const { pathname, query } = parse(asPath, true) + const { pathname: curPathname } = parse(this.asPath, true) + return curPathname !== pathname || !shallowEquals(query, this.query) } isShallowRoutingPossible (route) { diff --git a/test/integration/basic/pages/nav/as-path-pushstate.js b/test/integration/basic/pages/nav/as-path-pushstate.js index a5c1482b119f9..a85e0ac29afdb 100644 --- a/test/integration/basic/pages/nav/as-path-pushstate.js +++ b/test/integration/basic/pages/nav/as-path-pushstate.js @@ -9,7 +9,16 @@ export default withRouter(({router: {asPath, query}}) => { hello - +