Skip to content

Commit

Permalink
fix(next/router): Prevent query delete in routing when next.config ba…
Browse files Browse the repository at this point in the history
…sePath option is truthy (#40566)

## Bug

- [x] Related issues linked using `fixes #number`
    - fixes 
        - #38528 
        - #40432
- [x] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

Hi, it is my first pull request in this project.
So... if you need anything more tasks, please tell me.

Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
  • Loading branch information
flex-kyunghwa and ijjk committed Sep 15, 2022
1 parent 40b2d13 commit 7556611
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
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()
}
})
})

0 comments on commit 7556611

Please sign in to comment.