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

feat(react-router): added router option search.strict #2552

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/framework/react/api/router/RouterOptionsType.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ The `RouterOptions` type accepts an object with the following properties and met
- A function that will be used to parse search params when parsing the current location.
- Defaults to `defaultParseSearch`.

### `search.strict` property

- Type: `boolean`
- Optional
- Defaults to `false`
- Configures how unknown search params (= not returned by any `validateSearch`) are treated.
- If `false`, unknown search params will be kept.
- If `true`, unknown search params will be removed.

### `defaultPreload` property

- Type: `undefined | false | 'intent' | 'viewport'`
Expand Down
1 change: 1 addition & 0 deletions packages/react-router/src/Transitioner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export function Transitioner() {
params: true,
hash: true,
state: true,
_includeValidateSearch: true,
})

if (
Expand Down
19 changes: 17 additions & 2 deletions packages/react-router/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,11 +447,21 @@ export interface RouterOptions<
* While usually automatic, sometimes it can be useful to force the router into a server-side state, e.g. when using the router in a non-browser environment that has access to a global.document object.
*
* @default typeof document !== 'undefined'
* @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#isserver property)
* @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#isserver-property)
*/
isServer?: boolean

defaultSsr?: boolean

search?: {
/**
* Configures how unknown search params (= not returned by any `validateSearch`) are treated.
*
* @default false
* @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#search.strict-property)
*/
strict?: boolean
}
}

export interface RouterErrorSerializer<TSerializedError> {
Expand Down Expand Up @@ -1511,19 +1521,24 @@ export class Router<
let search = applyMiddlewares()

if (opts._includeValidateSearch) {
let validatedSearch = this.options.search?.strict ? {} : search
matchedRoutesResult?.matchedRoutes.forEach((route) => {
try {
if (route.options.validateSearch) {
const validator =
typeof route.options.validateSearch === 'object'
? route.options.validateSearch.parse
: route.options.validateSearch
search = { ...search, ...validator(search) }
validatedSearch = {
...validatedSearch,
...validator({ ...validatedSearch, ...search }),
}
}
} catch (e) {
// ignore errors here because they are already handled in matchRoutes
}
})
search = validatedSearch
}
search = replaceEqualDeep(fromSearch, search)
const searchStr = this.options.stringifySearch(search)
Expand Down
Loading