diff --git a/lib/router/router.js b/lib/router/router.js index 177457e6..61e6a07e 100644 --- a/lib/router/router.js +++ b/lib/router/router.js @@ -171,13 +171,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(pathname, query)) { + if (!this.urlIsNew(asPathname, asQuery)) { method = 'replaceState' } diff --git a/test/integration/basic/pages/nav/as-path-pushstate.js b/test/integration/basic/pages/nav/as-path-pushstate.js new file mode 100644 index 00000000..a5c1482b --- /dev/null +++ b/test/integration/basic/pages/nav/as-path-pushstate.js @@ -0,0 +1,17 @@ +import Link from 'next/link' +import {withRouter} from 'next/router' + +export default withRouter(({router: {asPath, query}}) => { + return
+}) diff --git a/test/integration/basic/test/client-navigation.js b/test/integration/basic/test/client-navigation.js index 2faafb0b..fcdbfa6a 100644 --- a/test/integration/basic/test/client-navigation.js +++ b/test/integration/basic/test/client-navigation.js @@ -633,6 +633,28 @@ export default (context, render) => { browser.close() }) }) + + describe('with next/link', () => { + it('should use pushState with same href and different asPath', async () => { + let browser + try { + browser = await webdriver(context.appPort, '/nav/as-path-pushstate') + await browser.elementByCss('#hello').click().waitForElementByCss('#something-hello') + const queryOne = JSON.parse(await browser.elementByCss('#router-query').text()) + expect(queryOne.something).toBe('hello') + await browser.elementByCss('#same-query').click().waitForElementByCss('#something-same-query') + const queryTwo = JSON.parse(await browser.elementByCss('#router-query').text()) + expect(queryTwo.something).toBe('hello') + await browser.back().waitForElementByCss('#something-hello') + const queryThree = JSON.parse(await browser.elementByCss('#router-query').text()) + expect(queryThree.something).toBe('hello') + } finally { + if (browser) { + browser.close() + } + } + }) + }) }) describe('runtime errors', () => {