Skip to content

Commit

Permalink
Make router UrlIsNew comparing method work as expected (vercel#6383)
Browse files Browse the repository at this point in the history
* make router UrlIsNew comparing method work as expected

* Remove shallow-equals from router and update urlIsNew check

* Remove shallow-equals test since it is no longer used

* Add integration test for asPath query
  • Loading branch information
tangye1234 authored and ijjk committed Feb 28, 2019
1 parent 42cff0a commit 704edcc
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 49 deletions.
5 changes: 1 addition & 4 deletions packages/next-server/lib/router/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import { parse } from 'url'
import mitt from '../mitt'
import shallowEquals from './shallow-equals'
import { loadGetInitialProps, getURL, formatWithValidation } from '../utils'

export default class Router {
Expand Down Expand Up @@ -363,9 +362,7 @@ export default class Router {
}

urlIsNew (asPath) {
const { pathname, query } = parse(asPath, true)
const { pathname: curPathname } = parse(this.asPath, true)
return curPathname !== pathname || !shallowEquals(query, this.query)
return this.asPath !== asPath
}

isShallowRoutingPossible (route) {
Expand Down
11 changes: 0 additions & 11 deletions packages/next-server/lib/router/shallow-equals.js

This file was deleted.

21 changes: 21 additions & 0 deletions test/integration/basic/pages/nav/as-path-query.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Link from 'next/link'
import { withRouter } from 'next/router'

export default withRouter(({ router: { asPath, query } }) => {
return <div
id={asPath
.replace('/', '').replace('/', '-').replace('?', '-').replace('=', '-')
}>
<div id='router-query'>{JSON.stringify(query)}</div>
<div>
<Link href='/nav/as-path-query?something=hello' as='/something/hello?something=hello'>
<a id='hello'>hello</a>
</Link>
</div>
<div>
<Link href='/nav/as-path-query?something=else' as='/something/hello?something=else'>
<a id='hello2'>hello</a>
</Link>
</div>
</div>
})
17 changes: 17 additions & 0 deletions test/integration/basic/test/client-navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,23 @@ export default (context) => {
}
}
})

it('should detect asPath query changes correctly', async () => {
let browser
try {
browser = await webdriver(context.appPort, '/nav/as-path-query')
await browser.elementByCss('#hello').click().waitForElementByCss('#something-hello-something-hello')
const queryOne = JSON.parse(await browser.elementByCss('#router-query').text())
expect(queryOne.something).toBe('hello')
await browser.elementByCss('#hello2').click().waitForElementByCss('#something-hello-something-else')
const queryTwo = JSON.parse(await browser.elementByCss('#router-query').text())
expect(queryTwo.something).toBe('else')
} finally {
if (browser) {
browser.close()
}
}
})
})
})

Expand Down
34 changes: 0 additions & 34 deletions test/unit/shallow-equal.test.js

This file was deleted.

0 comments on commit 704edcc

Please sign in to comment.