Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: setSearchParams() from useSearchParams() can add trailing slash to pathname in some circumstances #8312

Closed
Monstrel opened this issue Nov 10, 2021 · 3 comments
Labels

Comments

@Monstrel
Copy link

What version of React Router are you using?

6.0.2

Steps to Reproduce

With a BrowserRouter configured with a basename of /foo/bar and the browser pointed at site.com/foo/bar, use the setSearchParams function returned by the useSearchParams() hook to set search parameters such as:

setSearchParams({ name: 'Bob' })

Expected Behavior

The browser URL should update to site.com/foo/bar?name=Bob

Actual Behavior

The browser URL updates to site.com/foo/bar/?name=Bob

Note the trailing slash at the end of the path.

@Monstrel Monstrel added the bug label Nov 10, 2021
@senseibarni
Copy link
Contributor

senseibarni commented Mar 2, 2022

Just encountered the same behavior with version 6.2.1.

After short digging, I've found the cause in NavigateFunction which is returned by useNavigate hook:
if (basename !== "/") { path.pathname = joinPaths([basename, path.pathname]); }
and "extra" slash can be found in the joinPaths function:
const joinPaths = (paths: string[]): string => paths.join("/").replace(/\/\/+/g, "/");

@senseibarni
Copy link
Contributor

Created CodeSandbox for this: https://codesandbox.io/s/react-router-issue-8312-63qqc7
I think I will try to contribute to this one.

senseibarni pushed a commit to senseibarni/react-router that referenced this issue Mar 16, 2022
senseibarni pushed a commit to senseibarni/react-router that referenced this issue May 12, 2022
ryanflorence pushed a commit that referenced this issue Jun 21, 2022
* #8312 and also #6226

* cla sign

* fix: properly handle trailing slashes in Navigate and Link

* fix: handle useSearchParams use case with undefined to.pathname

Co-authored-by: Leszek Kobus <l.kobus@infinity-group.pl>
Co-authored-by: Matt Brophy <matt@brophy.org>
@brophdawg11
Copy link
Contributor

This should be fixed by #8861 - this should be included in the next 6.4.0 prerelease (hopefully this week)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants