nuqs icon indicating copy to clipboard operation
nuqs copied to clipboard

Nextjs basePath is being prepended as duplicate on useQueryStates state update.

Open aiverkeith opened this issue 2 years ago • 2 comments

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.

aiverkeith avatar Mar 31 '22 08:03 aiverkeith

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?

franky47 avatar Mar 31 '22 08:03 franky47

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
          );

aiverkeith avatar Mar 31 '22 09:03 aiverkeith

I am experiencing the same issue, are there any updates on this issue?

Mikaila94 avatar Oct 24 '22 07:10 Mikaila94

I just had an encounter with this bug in a client project, will take a look at it (and other related bugs) this week.

franky47 avatar Oct 24 '22 19:10 franky47

Seeing the same thing here, any news @franky47 ?

evsasse avatar Jan 12 '23 12:01 evsasse

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.

franky47 avatar Jan 12 '23 12:01 franky47

Just adding a +1 that I also came across this. Using @aiverkeith's temp fix for now.

nahiyankhan avatar Feb 05 '23 00:02 nahiyankhan

Fixed in #391, will be released in 1.10.2.

franky47 avatar Nov 10 '23 22:11 franky47