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

fix: don't overwrite searchParam values if they are provided in url with what is provided in validateSearch #2855

Closed
wants to merge 19 commits into from

Conversation

FoHoOV
Copy link

@FoHoOV FoHoOV commented Nov 25, 2024

Title is actually good enough :D
Fixes #2845
Fixes #2830

@schiller-manuel
Copy link
Contributor

can you please also have a a look at #2830 and #2799 as they seem related?

@FoHoOV
Copy link
Author

FoHoOV commented Nov 26, 2024

@schiller-manuel sure! As soon as I get back home

@FoHoOV
Copy link
Author

FoHoOV commented Nov 27, 2024

@schiller-manuel about #2830, yes its the same issue, this pr fixes that (and mine) but introduced another bug :D <Navigate ... search={{x:y}} /> wont add the sp. Gonna see what I did wrong :(
-- FIXED NOW

as for #2799, I don't beleive its related, since the passed object to search param is being overwritten (presumably deleting keys from it directly), if we change their example to this instead:

...
search={{...DefaultIndexSearch}} // create a new object here
...

it works as expected.

@FoHoOV FoHoOV marked this pull request as ready for review November 27, 2024 22:16
Copy link

nx-cloud bot commented Nov 28, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit 58eacb9. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this CI Pipeline Execution


✅ Successfully ran 2 targets

Sent with 💌 from NxCloud.

Copy link

pkg-pr-new bot commented Nov 28, 2024

Open in Stackblitz

More templates

@tanstack/arktype-adapter

npm i https://pkg.pr.new/@tanstack/arktype-adapter@2855

@tanstack/eslint-plugin-router

npm i https://pkg.pr.new/@tanstack/eslint-plugin-router@2855

@tanstack/create-router

npm i https://pkg.pr.new/@tanstack/create-router@2855

@tanstack/history

npm i https://pkg.pr.new/@tanstack/history@2855

@tanstack/react-cross-context

npm i https://pkg.pr.new/@tanstack/react-cross-context@2855

@tanstack/react-router

npm i https://pkg.pr.new/@tanstack/react-router@2855

@tanstack/react-router-with-query

npm i https://pkg.pr.new/@tanstack/react-router-with-query@2855

@tanstack/router-cli

npm i https://pkg.pr.new/@tanstack/router-cli@2855

@tanstack/router-devtools

npm i https://pkg.pr.new/@tanstack/router-devtools@2855

@tanstack/router-generator

npm i https://pkg.pr.new/@tanstack/router-generator@2855

@tanstack/router-plugin

npm i https://pkg.pr.new/@tanstack/router-plugin@2855

@tanstack/router-vite-plugin

npm i https://pkg.pr.new/@tanstack/router-vite-plugin@2855

@tanstack/start

npm i https://pkg.pr.new/@tanstack/start@2855

@tanstack/start-vite-plugin

npm i https://pkg.pr.new/@tanstack/start-vite-plugin@2855

@tanstack/valibot-adapter

npm i https://pkg.pr.new/@tanstack/valibot-adapter@2855

@tanstack/virtual-file-routes

npm i https://pkg.pr.new/@tanstack/virtual-file-routes@2855

@tanstack/zod-adapter

npm i https://pkg.pr.new/@tanstack/zod-adapter@2855

commit: 58eacb9

@@ -9,13 +9,14 @@ export function retainSearchParams<TSearchSchema extends object>(
return ({ search, next }) => {
const result = next(search)
if (keys === true) {
return { ...search, ...result }
return { ...result, ...search }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get this. retainSearchParams should only carry over search params that are NOT part of e.g. a <Link search={...}>. but this change here would mean that if the search params already contained foo: 'hello', then the value from <Link search={{foo:'bar'}}> would be ignored

Copy link
Author

@FoHoOV FoHoOV Nov 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it? The value is retained, whatever it's initialized to. Imagine you go to route x with param=p1, then goto x/y with search param=p2, this would mean the param is p2 when ur on x/y and reset back to p1 when go on x. This means the value is not retained on x/* route right? If this is not the intention (which can be achieved with validateSearth alone), I might have misunderstood the point.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, the intention of this search middleware is to keep search params around so they continue to live on with their previous value unless a Link overrides them

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooh, ok then. Feel free to close this PR, cuz I'm not home for next 2 or 3 days.

  • next(search) returns the validated result (including what's in the links search prop) so it's not gonna be as easy as this fix.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no rush, just convert it to draft for the time being

}
// add missing keys from search to result
// just like above, but only overwrite explicitly mentioned keys
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, result should have precedence

@FoHoOV FoHoOV marked this pull request as draft November 29, 2024 17:29
@FoHoOV
Copy link
Author

FoHoOV commented Dec 5, 2024

@schiller-manuel Currenly, search middlewares call next which calls searchValidator, meaning, searchValidator result is the input to retainSearchParams middleware. Validator might return a default x and then in our retainSearchParams, we say ok x exists (which is the result of validator), so lets keep it and ignore what we have right now. But, I think it should be the other way around, validator should use the result of middlewares not middlewares using the result of validator. Currenly we have this test:

const postsRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: 'posts',
  validateSearch: z.object({
    foo: z.string().default('default'),
  }),
  search: {
    middlewares: [
      // @ts-expect-error we cannot use zodValidator here to due to circular dependency
      // this means we cannot get the correct input type for this schema
      stripSearchParams({ foo: 'default' }),
      retainSearchParams(true),
    ],
  },

  component: () => {
    const { foo } = postsRoute.useSearch()
    return (
      <>
        <h1>Posts</h1>
        <div data-testid="posts-search">{foo}</div>
        <Link data-testid="posts-link-new" to="/posts/new">
          new
        </Link>
      </>
    )
  },
})

here we are explicitly saying that this route should have foo with default of default but also stripping it in the middlewares. I think, the searchValidator should have higher proirity over what our middleware is saying, middleware strips it, but the validator should add it back (then why write it in the first place? just dont set the default value for foo), for example if it was stripping foo with value x, it should remove x and our validator should add default to it which makes sense (IMHO), this way middlewares are in the middle and not in the end. If this searchValidatror was in the parent route instead, it would make sense that stripSearchParams should remove it, but having it on this route shoudln't. If it makes sense, and you are ok with it, I can make the change.

@FoHoOV FoHoOV marked this pull request as ready for review December 5, 2024 16:55
@schiller-manuel
Copy link
Contributor

then why write it in the first place? just dont set the default value for foo
the reason for that is that you can then rely in your code that the value is always set and you don't need to check for undefined

@FoHoOV
Copy link
Author

FoHoOV commented Dec 9, 2024

Hi, thanks for your reply.
This was my last possible solution that I could think of. I don't know any other solutions. I give up :D but the order of middlewares + searchValidator is really weird currently IMHO.
Thank you for your time >_<

@FoHoOV FoHoOV closed this Dec 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants