-
Notifications
You must be signed in to change notification settings - Fork 444
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(router): add support for search params #5094
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
1 Ignored Deployment
|
No changes to documentation |
Component Testing Report Updated Nov 3, 2023 12:17 PM (UTC)
|
Unit test changes: - improve error messages - add a few more test cases - update findMatchingNode tests with new internal format
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm worrying that the API is unconventional. Did you consider this approach?
Given the url:
/path/to/something?foo=bar&bar=baz
const router = useRouter() console.log(router.state, router.searchParams)Will log
{someParam: 'something'}, {foo: 'bar', bar: 'baz'}Navigate w/search params
router.navigate({ someParam: 'something', }, { searchParams: {foo: 'bar', bar: 'baz'} })
Can you elaborate on what you mean by unconventional, and what problems you think it may cause? I did consider it! But the url params is part of the state, so it felt natural to keep it there. It will make it easier to pass it around as a unified object representing the full picture of the current extracted url state. This, in addition to the fact that it would be harder to implement in a backwards compatible way (router.encode/decode is part of our public API), made me go for this solution, but open to reconsider if you can help me understand what real problems it can cause. If we didn't have to care about backwards compat, I think a better option might be if the state object itself had a distinction between url params and search params, e.g. using the example above: Given the url: const router = useRouter()
console.log(router.state) Will log (something like) {
pathParams: {
someParam: 'something'
},
searchParams: [['foo', 'bar'], ['bar', 'baz']]
} Noting this as a potential future improvement |
@bjoerge This part looks especially unconventional/foreign: _searchParams: [['foo', 'bar'], ['bar', 'baz']] Would be more readable like this: _searchParams: {foo: 'bar', bar: 'baz'} And ideally live outside of the |
It's completely valid to have multiple search params with the same name (which is why If we made it a record/map-like it would have to be: But as noted in the PR description, this format is aligned with what you get from Given that this PR is approved, I'll merge it for now. It should be trivial to convert the entries array into something more convenient, e.g. by using |
* chore(router): restore pre-v3 docs and test suite * test(router): add (failing) tests for urlSearchParams * feat(router): add support for search params Unit test changes: - improve error messages - add a few more test cases - update findMatchingNode tests with new internal format * fix(core): include search params when parsing url in workspace router
Description
This PR adds support for a special key on the url state:
_searchParams
, which is an array of[key: string, value: string]
tuples (similar to what you'd get fromURLSearchParams.entries()
).It uses the scope (aka namespace) feature of the router, so when you're within a scope you don't need to worry about how your own search params fits in with the global environment. In the url, they will be namespaced with
/some/route?scope[param]=value
. Params in nested scopes will bescopeA[scopeB][param]=value
(see added unit tests for details).Usage example
Given the route
/path/to/:someParam
Read search params
Given the url:
/path/to/something?foo=bar&bar=baz
Will log
Navigate w/search params
Will navigate to:
What to review
_
-prefix, which isn't strictly speaking safe as someone could potentially (although quite unlikely) have an existing route with_searchParams
as a param (e.g./path/to/:_searchParams
), which will then be in conflict. If we want to play it extra safe, we can consider changing to$searchParams
. We currently don't allow using$
in url params (i.e. defining a route as/path/to/:$param
throws an error).Note: this PR also recovers the test suite and readme that somehow didn't make it when we moved this from
@sanity/state-router
over to thesanity
-packageNotes for release
sanity/router