From 61c4cdb501eec8382c79b841c2a273846b8e9b53 Mon Sep 17 00:00:00 2001 From: Anthony Short Date: Wed, 24 Jun 2020 19:36:22 -0700 Subject: [PATCH] Avoid adding basePath when it's not needed (#14535) * Avoid adding basePath when it's not needed When using the `basePath` setting, on pages with params it will fire a router change. This will pass the url pathname in the `as` param using the `getUrl()` function. This means the `as` path will be sent through already including the `basePath`, leading to `/basePath/basePath/path` which will cause the router to throw an error. * lint * Add test case and ensure removal * Make sure to re-add before changeState Co-authored-by: JJ Kasper --- packages/next/client/index.js | 3 ++- packages/next/next-server/lib/router/router.ts | 2 +- test/integration/basepath/pages/[slug].js | 2 +- test/integration/basepath/test/index.test.js | 6 ++++++ 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/next/client/index.js b/packages/next/client/index.js index d3a4534f9bea5..9dae7d78ae445 100644 --- a/packages/next/client/index.js +++ b/packages/next/client/index.js @@ -9,6 +9,7 @@ import { RouterContext } from '../next-server/lib/router-context' import { isDynamicRoute } from '../next-server/lib/router/utils/is-dynamic' import * as envConfig from '../next-server/lib/runtime-config' import { getURL, loadGetInitialProps, ST } from '../next-server/lib/utils' +import { delBasePath } from '../next-server/lib/router/router' import initHeadManager from './head-manager' import PageLoader from './page-loader' import measureWebVitals from './performance-relayer' @@ -48,7 +49,7 @@ envConfig.setConfig({ publicRuntimeConfig: runtimeConfig || {}, }) -const asPath = getURL() +const asPath = delBasePath(getURL()) const pageLoader = new PageLoader(buildId, prefix, page) const register = ([r, f]) => pageLoader.registerPage(r, f) diff --git a/packages/next/next-server/lib/router/router.ts b/packages/next/next-server/lib/router/router.ts index 063cd653f7af2..a9120f335f28f 100644 --- a/packages/next/next-server/lib/router/router.ts +++ b/packages/next/next-server/lib/router/router.ts @@ -263,7 +263,7 @@ export default class Router implements BaseRouter { this.changeState( 'replaceState', formatWithValidation({ pathname: addBasePath(pathname), query }), - as + addBasePath(as) ) } diff --git a/test/integration/basepath/pages/[slug].js b/test/integration/basepath/pages/[slug].js index 7ad5b771f38ac..53205048d0044 100644 --- a/test/integration/basepath/pages/[slug].js +++ b/test/integration/basepath/pages/[slug].js @@ -1,3 +1,3 @@ import { useRouter } from 'next/router' -export default () =>

slug: {useRouter().query.slug}

+export default () =>

slug: {useRouter().query.slug}

diff --git a/test/integration/basepath/test/index.test.js b/test/integration/basepath/test/index.test.js index d4995dd9da3e7..c9d220b74212d 100644 --- a/test/integration/basepath/test/index.test.js +++ b/test/integration/basepath/test/index.test.js @@ -116,6 +116,12 @@ const runTests = (context, dev = false) => { }) } + it('should update dynamic params after mount correctly', async () => { + const browser = await webdriver(context.appPort, '/docs/hello-dynamic') + const text = await browser.elementByCss('#slug').text() + expect(text).toContain('slug: hello-dynamic') + }) + it('should navigate to index page with getStaticProps', async () => { const browser = await webdriver(context.appPort, '/docs/hello') await browser.eval('window.beforeNavigate = "hi"')