未验证 提交 61c4cdb5 编写于 作者: A Anthony Short 提交者: GitHub

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: NJJ Kasper <jj@jjsweb.site>
上级 fe529c49
......@@ -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)
......
......@@ -263,7 +263,7 @@ export default class Router implements BaseRouter {
this.changeState(
'replaceState',
formatWithValidation({ pathname: addBasePath(pathname), query }),
as
addBasePath(as)
)
}
......
import { useRouter } from 'next/router'
export default () => <p>slug: {useRouter().query.slug}</p>
export default () => <p id="slug">slug: {useRouter().query.slug}</p>
......@@ -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"')
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册