diff --git a/packages/next-server/lib/router/router.ts b/packages/next-server/lib/router/router.ts index b985f5c08f76..86a0c26a3160 100644 --- a/packages/next-server/lib/router/router.ts +++ b/packages/next-server/lib/router/router.ts @@ -215,6 +215,7 @@ export default class Router implements BaseRouter { // If the url change is only related to a hash change // We should not proceed. We should only change the state. if (this.onlyAHashChange(as)) { + this.asPath = as Router.events.emit('hashChangeStart', as) this.changeState(method, url, as) this.scrollToHash(as) diff --git a/test/integration/client-navigation/test/index.test.js b/test/integration/client-navigation/test/index.test.js index c204046cb64c..822558c60a8c 100644 --- a/test/integration/client-navigation/test/index.test.js +++ b/test/integration/client-navigation/test/index.test.js @@ -420,6 +420,18 @@ describe('Client Navigation', () => { await browser.close() }) + + it('should not run getInitialProps when removing via back', async () => { + const browser = await webdriver(context.appPort, '/nav/hash-changes') + + const counter = await browser + .elementByCss('#scroll-to-item-400').click() + .back() + .elementByCss('p').text() + + expect(counter).toBe('COUNT: 0') + await browser.close() + }) }) describe('when hash set to empty', () => {