From c591343531455deb088353e21d0b9abca5774b75 Mon Sep 17 00:00:00 2001 From: Jan Potoms <2109932+Janpot@users.noreply.github.com> Date: Sat, 4 Jul 2020 17:48:44 +0200 Subject: [PATCH] Fix hash change --- packages/next/next-server/lib/router/router.ts | 10 ++++------ test/integration/basepath/pages/hello.js | 5 +++++ test/integration/basepath/test/index.test.js | 16 ++++++++++++++++ 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/packages/next/next-server/lib/router/router.ts b/packages/next/next-server/lib/router/router.ts index 293af31acec1d..601703a1f367e 100644 --- a/packages/next/next-server/lib/router/router.ts +++ b/packages/next/next-server/lib/router/router.ts @@ -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 @@ -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) } diff --git a/test/integration/basepath/pages/hello.js b/test/integration/basepath/pages/hello.js index 082343638caa5..ab471c1dad506 100644 --- a/test/integration/basepath/pages/hello.js +++ b/test/integration/basepath/pages/hello.js @@ -65,5 +65,10 @@ export default () => (

Error route

+ + +

Hash change

+
+ ) diff --git a/test/integration/basepath/test/index.test.js b/test/integration/basepath/test/index.test.js index 85cb7911b137c..96082d24808d4 100644 --- a/test/integration/basepath/test/index.test.js +++ b/test/integration/basepath/test/index.test.js @@ -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 {