Skip to content

feat: Callback to process merged URLSearchParams on update #1109

@franky47

Description

@franky47

Let's move the discussion from #1066 here.

The idea is to introduce a callback as an adapter prop that will be called after the URLSearchParams have been merged:

for (const [key, value] of items) {
if (value === null) {
search.delete(key)
} else {
search.set(key, value)
}
}

It should run before the URLSearchParams object is sent to the adapter for updating the URL.

This can be used to implement a few interesting behaviours:

  • Sorting the search params in some way (eg: alphabetically by key)
  • Injecting / removing values
  • Other kinds of processing

Example API:

<NuqsAdapter 
  processUrlSearchParams={(search) => {
    const entries = Array.from(params.entries())
    entries.sort(([a], [b]) => a.localeCompare(b))
    return new URLSearchParams(entries)
  }}
>
  { children }
</NuqsAdapter>

Message history

(like a callback function to process URLSearchParams after applying updates for alphabetical sorting).

Is this feature planned? We're definitely looking for this feature specifically. (alphabetical sorting of parameters)
Edit: or a callback where we can do it ourselves.

Originally posted by @Multiply in #1066 (comment)


@Multiply would you like to try your hand at a PR?

Was already looking into the recent introduction of global default config.
Do you have a preference for where the middleware should be called?

Originally posted by @Multiply in #1066 (comment)

Related discussions

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions