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

Search middleware break when search validation fails #3335

Open
emilbruckner opened this issue Feb 5, 2025 · 1 comment
Open

Search middleware break when search validation fails #3335

emilbruckner opened this issue Feb 5, 2025 · 1 comment

Comments

@emilbruckner
Copy link

Which project does this relate to?

Router

Describe the bug

Using retainSearchParams, when the search validation fails, unexpected options are passed to retainSearchParams and it throws an unexpected error.
Without using retainSearchParams, a failed search validation redirects or shows an error component.

Your Example Website or App

https://stackblitz.com/edit/github-f8tcj4j5?file=src%2Froutes%2F__root.tsx

Steps to Reproduce the Bug or Issue

  1. console error in the example, no error component shown
  2. remove the middleware (line 20), and rerun → error shown

Expected behavior

I’d expect the same behaviour with and without that middleware.

Screenshots or Videos

No response

Platform

macos chromium

Additional context

No response

@rafaelchiti
Copy link

Experiencing the same:
Uncaught TypeError: Cannot use 'in' operator to search for 'whatever-query-param' in undefined

Looks like the retainSearchParams is not handling when there are none (result is 'undefined')

export function retainSearchParams<TSearchSchema extends object>(
  keys: Array<keyof TSearchSchema> | true,
): SearchMiddleware<TSearchSchema> {
  return ({ search, next }) => {
    const result = next(search)
    if (keys === true) {
      return { ...search, ...result }
    }
    // add missing keys from search to result
    keys.forEach((key) => {
      if (!(key in result)) {
        result[key] = search[key]
      }
    })
    return result
  }
}

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

No branches or pull requests

2 participants