Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(next/router): Prevent query delete in routing when next.config basePath option is truthy #40566

Merged
merged 4 commits into from Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/next/shared/lib/router/router.ts
Expand Up @@ -1552,7 +1552,11 @@ export default class Router implements BaseRouter {
query = Object.assign({}, routeInfo.query || {}, query)
}

if (routeMatch && pathname !== parsed.pathname) {
const cleanedParsedPathname = hasBasePath(parsed.pathname)
? removeBasePath(parsed.pathname)
: parsed.pathname

if (routeMatch && pathname !== cleanedParsedPathname) {
Object.keys(routeMatch).forEach((key) => {
if (routeMatch && query[key] === routeMatch[key]) {
delete query[key]
Expand Down
@@ -0,0 +1,11 @@
import { useRouter } from 'next/router'

export default function DynamicRoutes() {
const { query } = useRouter()

return (
<main>
<h1 id="route-name">{query.routeName}</h1>
</main>
)
}
5 changes: 5 additions & 0 deletions test/e2e/middleware-base-path/app/pages/index.js
Expand Up @@ -27,6 +27,11 @@ export default function Main({ message }) {
<a>redirect me to about</a>
</Link>
</li>
<li>
<Link href="/dynamic-routes/hello-world">
<a id="go-to-hello-world-anchor">Hello World</a>
</Link>
</li>
</ul>
</div>
)
Expand Down
10 changes: 10 additions & 0 deletions test/e2e/middleware-base-path/test/index.test.ts
Expand Up @@ -35,4 +35,14 @@ describe('Middleware base tests', () => {
const $ = cheerio.load(html)
expect($('.title').text()).toBe('About Page')
})
it('router.query must exist when Link clicked page routing', async () => {
const browser = await webdriver(next.url, '/root')
try {
await browser.elementById('go-to-hello-world-anchor').click()
const routeName = await browser.elementById('route-name').text()
expect(routeName).toMatch('hello-world')
} finally {
await browser.close()
}
})
})