Skip to content

Commit

Permalink
Fix hash change
Browse files Browse the repository at this point in the history
  • Loading branch information
Janpot committed Jul 4, 2020
1 parent 6436350 commit c591343
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
10 changes: 4 additions & 6 deletions packages/next/next-server/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,7 @@ export default class Router implements BaseRouter {
// until after mount to prevent hydration mismatch
this.asPath =
// @ts-ignore this is temporarily global (attached to window)
isDynamicRoute(pathname) && __NEXT_DATA__.autoExport
? pathname
: delBasePath(as)
isDynamicRoute(pathname) && __NEXT_DATA__.autoExport ? pathname : as
this.basePath = basePath
this.sub = subscription
this.clc = null
Expand Down Expand Up @@ -408,11 +406,11 @@ export default class Router implements BaseRouter {
// WARNING: `_h` is an internal option for handing Next.js client-side
// hydration. Your app should _never_ use this property. It may change at
// any time without notice.
if (!options._h && this.onlyAHashChange(as)) {
this.asPath = as
if (!options._h && this.onlyAHashChange(cleanedAs)) {
this.asPath = cleanedAs
Router.events.emit('hashChangeStart', cleanedAs)
this.changeState(method, url, as, options)
this.scrollToHash(as)
this.scrollToHash(cleanedAs)
Router.events.emit('hashChangeComplete', cleanedAs)
return resolve(true)
}
Expand Down
5 changes: 5 additions & 0 deletions test/integration/basepath/pages/hello.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,10 @@ export default () => (
<h1>Error route</h1>
</a>
</Link>
<Link href="/hello#some-hash">
<a id="hash-change">
<h1>Hash change</h1>
</a>
</Link>
</>
)
16 changes: 16 additions & 0 deletions test/integration/basepath/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,22 @@ const runTests = (context, dev = false) => {
}
})

it('should use urls without basepath in router events for hash changes', async () => {
const browser = await webdriver(context.appPort, '/docs/hello')
try {
await browser.eval('window._clearEventLog()')
await browser.elementByCss('#hash-change').click()

const eventLog = await browser.eval('window._getEventLog()')
expect(eventLog).toEqual([
['hashChangeStart', '/hello#some-hash'],
['hashChangeComplete', '/hello#some-hash'],
])
} finally {
await browser.close()
}
})

it('should use urls without basepath in router events for cancelled routes', async () => {
const browser = await webdriver(context.appPort, '/docs/hello')
try {
Expand Down

0 comments on commit c591343

Please sign in to comment.