Skip to content

Commit 4df98f9

Browse files
fix: 🐛 dynamic query parameters
1 parent 7740c25 commit 4df98f9

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/query-parameter-store.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,27 @@ const createStore = <P extends z.ZodType>(
9191
if (parsedParams.success) {
9292
const pushOrReplace = options.replace || routerOptions.replace ? Router.replace : Router.push
9393

94+
// Extract dynamic route parameters
95+
const dynamicParams = Object.keys(Router.query).reduce(
96+
(acc, key) => {
97+
if (Router.pathname.includes(`[${key}]`) || Router.pathname.includes(`[...${key}]`)) {
98+
acc[key] = Router.query[key]
99+
}
100+
return acc
101+
},
102+
{} as Record<string, string | string[] | undefined>,
103+
)
104+
105+
// Merge dynamic params with new params, prioritizing dynamic params
106+
const mergedParams = {
107+
...parsedParams.data,
108+
...dynamicParams,
109+
} as z.infer<P>
110+
94111
pushOrReplace(
95112
{
96113
pathname: Router.pathname,
97-
query: parsedParams.data as z.infer<P>,
114+
query: mergedParams,
98115
},
99116
undefined,
100117
pick(

0 commit comments

Comments
 (0)