diff --git a/packages/next/next-server/lib/router/router.ts b/packages/next/next-server/lib/router/router.ts
index 28741a5379029..966cd34be0988 100644
--- a/packages/next/next-server/lib/router/router.ts
+++ b/packages/next/next-server/lib/router/router.ts
@@ -379,6 +379,10 @@ export default class Router implements BaseRouter {
let url = typeof _url === 'object' ? formatWithValidation(_url) : _url
let as = typeof _as === 'object' ? formatWithValidation(_as) : _as
+ // parse url parts without basePath since pathname should map 1-1 with
+ // pages dir
+ const { pathname, query, protocol } = parse(delBasePath(url), true)
+
url = addBasePath(url)
as = addBasePath(as)
@@ -410,8 +414,6 @@ export default class Router implements BaseRouter {
return resolve(true)
}
- const { pathname, query, protocol } = parse(url, true)
-
if (!pathname || protocol) {
if (process.env.NODE_ENV !== 'production') {
throw new Error(
diff --git a/test/integration/basepath/pages/gsp.js b/test/integration/basepath/pages/gsp.js
index 9fa6a9ad4e5ab..fd6575b8b15e6 100644
--- a/test/integration/basepath/pages/gsp.js
+++ b/test/integration/basepath/pages/gsp.js
@@ -1,3 +1,5 @@
+import { useRouter } from 'next/router'
+
export const getStaticProps = () => {
return {
props: {
@@ -11,5 +13,6 @@ export default (props) => (
<>
getStaticProps
{JSON.stringify(props)}
+ {useRouter().pathname}
>
)
diff --git a/test/integration/basepath/pages/gssp.js b/test/integration/basepath/pages/gssp.js
index f7686677925e5..83737eadd4eaf 100644
--- a/test/integration/basepath/pages/gssp.js
+++ b/test/integration/basepath/pages/gssp.js
@@ -1,3 +1,5 @@
+import { useRouter } from 'next/router'
+
export const getServerSideProps = () => {
return {
props: {
@@ -11,5 +13,6 @@ export default (props) => (
<>
getServerSideProps
{JSON.stringify(props)}
+ {useRouter().pathname}
>
)
diff --git a/test/integration/basepath/pages/hello.js b/test/integration/basepath/pages/hello.js
index 97382993820a3..461ba5993724e 100644
--- a/test/integration/basepath/pages/hello.js
+++ b/test/integration/basepath/pages/hello.js
@@ -21,5 +21,6 @@ export default () => (
{useRouter().basePath}
+ {useRouter().pathname}
>
)
diff --git a/test/integration/basepath/test/index.test.js b/test/integration/basepath/test/index.test.js
index 56ca6080f057d..1fc675e82eb32 100644
--- a/test/integration/basepath/test/index.test.js
+++ b/test/integration/basepath/test/index.test.js
@@ -59,17 +59,23 @@ const runTests = (context, dev = false) => {
const props = JSON.parse(await browser.elementByCss('#props').text())
expect(props.hello).toBe('world')
+
+ const pathname = await browser.elementByCss('#pathname').text()
+ expect(pathname).toBe('/gsp')
})
it('should fetch data for getServerSideProps without reloading', async () => {
const browser = await webdriver(context.appPort, '/docs/hello')
await browser.eval('window.beforeNavigate = true')
- await browser.elementByCss('#gsp-link').click()
- await browser.waitForElementByCss('#gsp')
+ await browser.elementByCss('#gssp-link').click()
+ await browser.waitForElementByCss('#gssp')
expect(await browser.eval('window.beforeNavigate')).toBe(true)
const props = JSON.parse(await browser.elementByCss('#props').text())
expect(props.hello).toBe('world')
+
+ const pathname = await browser.elementByCss('#pathname').text()
+ expect(pathname).toBe('/gssp')
})
it('should have correct href for a link', async () => {
@@ -118,6 +124,24 @@ const runTests = (context, dev = false) => {
await browser.close()
}
})
+
+ it('should allow URL query strings without refresh', async () => {
+ const browser = await webdriver(context.appPort, '/docs/hello?query=true')
+ try {
+ await browser.eval('window.itdidnotrefresh = "hello"')
+ await new Promise((resolve, reject) => {
+ // Timeout of EventSource created in setupPing()
+ // (on-demand-entries-utils.js) is 5000 ms (see #13132, #13560)
+ setTimeout(resolve, 10000)
+ })
+ expect(await browser.eval('window.itdidnotrefresh')).toBe('hello')
+
+ const pathname = await browser.elementByCss('#pathname').text()
+ expect(pathname).toBe('/hello')
+ } finally {
+ await browser.close()
+ }
+ })
}
describe('basePath development', () => {