diff --git a/docs/api-reference/next/router.md b/docs/api-reference/next/router.md index cd474b4112735..db22d750d5bd0 100644 --- a/docs/api-reference/next/router.md +++ b/docs/api-reference/next/router.md @@ -318,6 +318,8 @@ You can listen to different events happening inside the Next.js Router. Here's a - `hashChangeComplete(url)` - Fires when the hash has changed but not the page > Here `url` is the URL shown in the browser. If you call `router.push(url, as)` (or similar), then the value of `url` will be `as`. +> +> **Note:** If you [configure a `basePath`](/docs/api-reference/next.config.js/basepath.md) then the value of `url` will be `basePath + as`. #### Usage diff --git a/packages/next/next-server/lib/router/router.ts b/packages/next/next-server/lib/router/router.ts index 2c5d2e175cbb5..951e64f82cfda 100644 --- a/packages/next/next-server/lib/router/router.ts +++ b/packages/next/next-server/lib/router/router.ts @@ -451,7 +451,7 @@ export default class Router implements BaseRouter { } const cleanedAs = delBasePath(as) - this._inFlightRoute = cleanedAs + this._inFlightRoute = as // If the url change is only related to a hash change // We should not proceed. We should only change the state. @@ -461,10 +461,10 @@ export default class Router implements BaseRouter { // any time without notice. if (!options._h && this.onlyAHashChange(cleanedAs)) { this.asPath = cleanedAs - Router.events.emit('hashChangeStart', cleanedAs) + Router.events.emit('hashChangeStart', as) this.changeState(method, url, as, options) this.scrollToHash(cleanedAs) - Router.events.emit('hashChangeComplete', cleanedAs) + Router.events.emit('hashChangeComplete', as) return true } @@ -524,7 +524,7 @@ export default class Router implements BaseRouter { } } - Router.events.emit('routeChangeStart', cleanedAs) + Router.events.emit('routeChangeStart', as) // If shallow is true and the route exists in the router cache we reuse the previous result return this.getRouteInfo(route, pathname, query, as, shallow).then( @@ -537,11 +537,11 @@ export default class Router implements BaseRouter { } if (error && error[ABORTED]) { - Router.events.emit('routeChangeError', error, cleanedAs) + Router.events.emit('routeChangeError', error, as) return false } - Router.events.emit('beforeHistoryChange', cleanedAs) + Router.events.emit('beforeHistoryChange', as) this.changeState(method, url, as, options) if (process.env.NODE_ENV !== 'production') { @@ -563,7 +563,7 @@ export default class Router implements BaseRouter { window.scrollTo(options._N_X, options._N_Y) } } - Router.events.emit('routeChangeComplete', cleanedAs) + Router.events.emit('routeChangeComplete', as) return true }