diff --git a/packages/next/next-server/lib/router/router.ts b/packages/next/next-server/lib/router/router.ts index aa482c48e9a4d..825c85c79de7f 100644 --- a/packages/next/next-server/lib/router/router.ts +++ b/packages/next/next-server/lib/router/router.ts @@ -488,6 +488,7 @@ export default class Router implements BaseRouter { Router.events.emit('hashChangeStart', as) this.changeState(method, url, as, options) this.scrollToHash(cleanedAs) + this.notify(this.components[this.route]) Router.events.emit('hashChangeComplete', as) return true } diff --git a/test/integration/client-navigation/pages/nav/hash-changes.js b/test/integration/client-navigation/pages/nav/hash-changes.js index d09ebb6163d6d..fb87ee235fb89 100644 --- a/test/integration/client-navigation/pages/nav/hash-changes.js +++ b/test/integration/client-navigation/pages/nav/hash-changes.js @@ -1,53 +1,58 @@ -import React, { Component } from 'react' +import React from 'react' import Link from 'next/link' +import { useRouter } from 'next/router' let count = 0 -export default class SelfReload extends Component { - static getInitialProps({ res }) { - if (res) return { count: 0 } - count += 1 +const HashChanges = ({ count }) => { + const router = useRouter() - return { count } - } + return ( +
+ + Via Link + + + Via A + + + Page URL + + + Via Empty Hash + + + Go to item 400 + + + Go to name item 400 + +

COUNT: {count}

+ {Array.from({ length: 500 }, (x, i) => i + 1).map((i) => { + return ( +
+ {i} +
+ ) + })} + {Array.from({ length: 500 }, (x, i) => i + 1).map((i) => { + return ( +
+ {i} +
+ ) + })} +
ASPATH: {router.asPath}
+
PATHNAME: {router.pathname}
+
+ ) +} + +HashChanges.getInitialProps = ({ res }) => { + if (res) return { count: 0 } + count += 1 - render() { - return ( -
- - Via Link - - - Via A - - - Page URL - - - Via Empty Hash - - - Go to item 400 - - - Go to name item 400 - -

COUNT: {this.props.count}

- {Array.from({ length: 500 }, (x, i) => i + 1).map((i) => { - return ( -
- {i} -
- ) - })} - {Array.from({ length: 500 }, (x, i) => i + 1).map((i) => { - return ( -
- {i} -
- ) - })} -
- ) - } + return { count } } + +export default HashChanges diff --git a/test/integration/client-navigation/test/index.test.js b/test/integration/client-navigation/test/index.test.js index 68653125795db..1e2c7c8bac347 100644 --- a/test/integration/client-navigation/test/index.test.js +++ b/test/integration/client-navigation/test/index.test.js @@ -497,8 +497,6 @@ describe('Client Navigation', () => { .click() .eval('window.pageYOffset') - console.log(scrollPosition) - expect(scrollPosition).toBe(16258) // Scrolls back to top when scrolling to `#` with no value. @@ -534,6 +532,22 @@ describe('Client Navigation', () => { } } }) + + it('Should update asPath', async () => { + let browser + try { + browser = await webdriver(context.appPort, '/nav/hash-changes') + + await browser.elementByCss('#via-link').click() + + const asPath = await browser.elementByCss('div#asPath').text() + expect(asPath).toBe('ASPATH: /nav/hash-changes#via-link') + } finally { + if (browser) { + await browser.close() + } + } + }) }) describe('when hash change via A tag', () => {