nuqs
nuqs copied to clipboard
Nextjs basePath is being prepended as duplicate on useQueryStates state update.
I had an issue where if you have basePath: "subdomain"
on your next.config.js, whenever the state updates using useQueryStates setter function the basePath is being duplicated.
https://www.domain.com/subdomain/path?state=1
becomes https://www.domain.com/subdomain/subdomain/path/?state=1
I temporarily fixed it by using the router.pathname
instead of window.location.pathname
on the as parameter. I'm wondering what's the reason of using window pathname instead of from the route.
Thanks for catching that!
So your fix was this?
return updateUrl?.call(
router,
{
pathname: router.pathname,
hash,
search
},
{
- pathname: window.location.pathname,
+ pathname: router.pathname,
hash,
search
},
transitionOptions
)
The reason why I was using the window.location.pathname
had to do with keeping a referentially stable state updater function (not have it regenerate on router property changes), but since we're still using the pathname for the first argument I don't see why we can't use it as the as
parameter. Might have to check that dynamic routes still resolve correctly with that fix, is that a use-case you have in your app?
Yes, our use case is to host the Nextjs app on a subdomain (https://domain.com/subdomain) with a basePath set on next.config.js
While exploring the reason why window.location.pathname
is used, I encountered this https://github.com/47ng/next-usequerystate/issues/282 So that my basePath fix will still continue with dynamic routing/slug, I changed it to
updateUrl.call(
router,
{
pathname: router.pathname,
hash,
search,
},
{
pathname: window.location.pathname.replace(router.basePath, ""),
hash,
search,
},
transitionOptions
);
I am experiencing the same issue, are there any updates on this issue?
I just had an encounter with this bug in a client project, will take a look at it (and other related bugs) this week.
Seeing the same thing here, any news @franky47 ?
Unfortunately I did not have time to look at it.
If I can fit some maintenance time on a client contract that uses this library I'll give it a shot, but until then I'll happily review external contributions.
Just adding a +1 that I also came across this. Using @aiverkeith's temp fix for now.
Fixed in #391, will be released in 1.10.2
.