From 755661144979db84e44caa5bb66b62e350f8a107 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=EC=9C=A0=EA=B2=BD=ED=99=94?=
<51588680+flex-kyunghwa@users.noreply.github.com>
Date: Fri, 16 Sep 2022 07:56:53 +0900
Subject: [PATCH] fix(next/router): Prevent query delete in routing when
next.config basePath 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>
---
packages/next/shared/lib/router/router.ts | 6 +++++-
.../app/pages/dynamic-routes/[routeName].js | 11 +++++++++++
test/e2e/middleware-base-path/app/pages/index.js | 5 +++++
test/e2e/middleware-base-path/test/index.test.ts | 10 ++++++++++
4 files changed, 31 insertions(+), 1 deletion(-)
create mode 100644 test/e2e/middleware-base-path/app/pages/dynamic-routes/[routeName].js
diff --git a/packages/next/shared/lib/router/router.ts b/packages/next/shared/lib/router/router.ts
index cf1180fd6766ee0..cda9125962a0719 100644
--- a/packages/next/shared/lib/router/router.ts
+++ b/packages/next/shared/lib/router/router.ts
@@ -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]
diff --git a/test/e2e/middleware-base-path/app/pages/dynamic-routes/[routeName].js b/test/e2e/middleware-base-path/app/pages/dynamic-routes/[routeName].js
new file mode 100644
index 000000000000000..1fe4784b683d4e2
--- /dev/null
+++ b/test/e2e/middleware-base-path/app/pages/dynamic-routes/[routeName].js
@@ -0,0 +1,11 @@
+import { useRouter } from 'next/router'
+
+export default function DynamicRoutes() {
+ const { query } = useRouter()
+
+ return (
+ {query.routeName}
+