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

Reset search with question mark value to the empty string #701

Open
vimkin opened this issue May 9, 2019 · 3 comments
Open

Reset search with question mark value to the empty string #701

vimkin opened this issue May 9, 2019 · 3 comments

Comments

@vimkin
Copy link

vimkin commented May 9, 2019

Hi, I would like to suggest a feature here. Currently I'm using react-router-dom for one of my projects, which of course uses history dependency under the hood. Both push and replace methods have two possible overloads and may be executed whether with path as string or location descriptor as object. Using push method with path works as expected and suppresses difference between f.e. /about and /about?, whereas push called with location descriptor differentiate search values such as ""(empty string) and ? which causes redundant update issues in environment where you f.e. execute data fetching on search change, so that location.search equals to ? after such navigate action. Wouldn't it be applicable to reset next search from ? to "" --even if ? was explicitly set-- directly in history library to prevent such a case?

For more detailed example you might have a look on this example with react-router-dom. Just click the navigations links and observe how fetch counter is increased.

@vimkin vimkin closed this as completed May 9, 2019
@vimkin vimkin changed the title Reset Reset search with question mark value to the empty string May 9, 2019
@vimkin vimkin reopened this May 9, 2019
@StringEpsilon
Copy link

I have an alternative parsePath implementation (based on Regex) lying around, which coincedentially has the desired behavior:

function parsePath(path) {
  const parts = path.match(/(\/?[^?#]*)(\?[^#?]+)?(\#[^#?]+)?/);
  if (parts && parts.length === 4) {
    return {
      pathname: parts[1] || "/",
      search: parts[2] || "",
      hash: parts[3] || ""
    };
  }
  return {
    pathname: "/",
    search: "",
    hash: ""
  };
}

I'll submit a PR later.

@mjackson
Copy link
Member

Thank you for the excellent repro, @vadikace. I think I'd consider this a bug. Essentially what it boils down to is you've got 2 links:

<Link to="/?">one</Link>
<Link to={{ pathname: '/', search: '?' }}>two</Link>

In both cases those links take you to /, but in when the object is used the location.search is ?. That's the bug IMO.

I'd be happy to accept a PR that fixes this.

@mjackson mjackson mentioned this issue Sep 13, 2019
25 tasks
@vimkin
Copy link
Author

vimkin commented Oct 16, 2019

I'll get my hands on it soonish

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

Successfully merging a pull request may close this issue.

3 participants