-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Add parameter route string to routes object. #7469
Conversation
Thanks for the PR! I'm always excited for new router related features 🙂 In the meantime, could you give a more detailed use case for this? How/where/why would you be using it? |
16 replays were recorded for ece7b76. 16 Passed
requireAuth graphql checks
|
Vielen Dank für das Beispiel. This adds to the public "api" of Redwood, so I want to get the team's input on syntax and stuff. I'll try to ask next week |
@dennemark We talked about this one on our Core team meeting. We all agreed your use case makes sense though. Just wondering if there is another way we could help you solve it. Can we give you better tools to make this easier for you? So you don't manually have to compare raw strings. But even if we do write a better/another |
Would this be an alternative? Call routes.myRoute() without any parameters, to get the original path. Or write routes.myRoute(true) or something like it? |
@dennemark We discussed this PR again today. Currently leaning towards this syntax. Please let us know what you think
( Another possibility is |
Per conversation with @thedavidprice, @Tobbe and I will take lead on the design decisions for this one. @dennemark are you still interested in working on this? Please see Tobbe's last comment above |
@jtoar you can count me in.
|
Awesome! 🙂
Adding a second parameter to the function was also considered in #8069 for adding |
So we stick to variant 1
or
right? |
@jtoar and I have a meeting scheduled later today. I'll make sure we discuss this PR. Another option I just came up with, that might be more clear than "any" is |
Hey @dennemark, @Tobbe and I settled on import { routes, RouteParams } from `@redwoodjs/router
routes.example({ dynamicParameter: 'dog', literalParameter: RouteParams.LITERAL }) We don't want to attach any more properties to routes because they could conflict with the dynamic properties on routes. (While it's unlikely anyone names a route I'm not sure if you need a Symbol for |
Yes, looks good to me 🙂 |
Hey @dennemark, were you still interested in this one? No pressure, just didn't want it to get lost |
Hey! Yes i still have it on my list. But these days its not easy to find time... |
@dennemark Thanks for checking in. No rush on getting it done. But do let us know if you want us to finish it up for you |
@dennemark Thanks for sticking with this one 🙂 I rewrote your test a little bit, and added a few new test cases to the test. One of the new ones is failing. I'll discuss it with Dom on Monday. |
[![Mend Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com) This PR contains the following updates: | Package | Change | Age | Adoption | Passing | Confidence | |---|---|---|---|---|---| | [@npmcli/arborist](https://togithub.com/npm/cli) ([source](https://togithub.com/npm/cli/tree/HEAD/workspaces/arborist)) | [`6.5.0` -> `7.2.2`](https://renovatebot.com/diffs/npm/@npmcli%2farborist/6.5.0/7.2.2) | [![age](https://developer.mend.io/api/mc/badges/age/npm/@npmcli%2farborist/7.2.2?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@npmcli%2farborist/7.2.2?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@npmcli%2farborist/6.5.0/7.2.2?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@npmcli%2farborist/6.5.0/7.2.2?slim=true)](https://docs.renovatebot.com/merge-confidence/) | --- <details> <summary>npm/cli (@&redwoodjs#8203;npmcli/arborist)</summary> [`v7.2.2`](https://togithub.com/npm/cli/blob/HEAD/workspaces/arborist/CHANGELOG.md#722-2023-12-06) - [`ae2d982`](https://togithub.com/npm/cli/commit/ae2d98292472897b8365829633cd47a6cb006d03) [#&redwoodjs#8203;7027](https://togithub.com/npm/cli/pull/7027) arborist: `node.target` can be `null` when it is a file dep or symlink ([#&redwoodjs#8203;7027](https://togithub.com/npm/cli/issues/7027)) ([@&redwoodjs#8203;ljharb](https://togithub.com/ljharb), [@&redwoodjs#8203;lukekarrys](https://togithub.com/lukekarrys)) - [`f875caa`](https://togithub.com/npm/cli/commit/f875caa86900122819311dd77cde01c700fd1817) [#&redwoodjs#8203;6998](https://togithub.com/npm/cli/pull/6998) clean up shrinkwrap code ([#&redwoodjs#8203;6998](https://togithub.com/npm/cli/issues/6998)) ([@&redwoodjs#8203;wraithgar](https://togithub.com/wraithgar)) - [`f656b66`](https://togithub.com/npm/cli/commit/f656b669e549286844f2071b9b62cf23f7958034) [#&redwoodjs#8203;7062](https://togithub.com/npm/cli/pull/7062) `@npmcli/template-oss@4.21.3` ([#&redwoodjs#8203;7062](https://togithub.com/npm/cli/issues/7062)) ([@&redwoodjs#8203;lukekarrys](https://togithub.com/lukekarrys)) - [`9754b17`](https://togithub.com/npm/cli/commit/9754b173de26f3173e7f41eab34733fe9ba50f1d) [#&redwoodjs#8203;7051](https://togithub.com/npm/cli/pull/7051) use global npm for workspace tests ([@&redwoodjs#8203;lukekarrys](https://togithub.com/lukekarrys)) - [`3891757`](https://togithub.com/npm/cli/commit/3891757f54d6d960cbf5f0d93d183d6424e8bed6) [#&redwoodjs#8203;7051](https://togithub.com/npm/cli/pull/7051) `@npmcli/template-oss@4.21.2` ([@&redwoodjs#8203;lukekarrys](https://togithub.com/lukekarrys)) [`v7.2.1`](https://togithub.com/npm/cli/blob/HEAD/workspaces/arborist/CHANGELOG.md#721-2023-10-31) [Compare Source](https://togithub.com/npm/cli/compare/v7.2.0...84bf72e596eb8e8dcf62b2e508da3646828a5221) - [`dfb6298`](https://togithub.com/npm/cli/commit/dfb6298c3eb9fb7ef452906765ac5f23ea6fec49) [#&redwoodjs#8203;6937](https://togithub.com/npm/cli/pull/6937) `node-gyp@10.0.0` ([#&redwoodjs#8203;6937](https://togithub.com/npm/cli/issues/6937)) [`v7.2.0`](https://togithub.com/npm/cli/blob/HEAD/workspaces/arborist/CHANGELOG.md#720-2023-10-02) [Compare Source](https://togithub.com/npm/cli/compare/v7.1.0...v7.2.0) - [`81a460f`](https://togithub.com/npm/cli/commit/81a460f6e6317aca2288d16cda591aa6541540c6) [#&redwoodjs#8203;6732](https://togithub.com/npm/cli/pull/6732) add package-lock-only mode to npm query ([@&redwoodjs#8203;wraithgar](https://togithub.com/wraithgar)) - [`0d29855`](https://togithub.com/npm/cli/commit/0d2985535c9cc3dfc3e1f355580570c9cce37d61) [#&redwoodjs#8203;6732](https://togithub.com/npm/cli/pull/6732) add no-package-lock mode to npm audit ([@&redwoodjs#8203;wraithgar](https://togithub.com/wraithgar)) - [`0860159`](https://togithub.com/npm/cli/commit/0860159e18aa0fa985ef53fcfe0a57fbda995efb) [#&redwoodjs#8203;6829](https://togithub.com/npm/cli/pull/6829) ensure workspace links query parents correctly ([#&redwoodjs#8203;6829](https://togithub.com/npm/cli/issues/6829)) ([@&redwoodjs#8203;Carl-Foster](https://togithub.com/Carl-Foster)) - [`bef7481`](https://togithub.com/npm/cli/commit/bef7481282f18f5b8ad864dc76669801187029fe) [#&redwoodjs#8203;6782](https://togithub.com/npm/cli/pull/6782) query with workspace descendents ([#&redwoodjs#8203;6782](https://togithub.com/npm/cli/issues/6782)) ([@&redwoodjs#8203;bdehamer](https://togithub.com/bdehamer)) - [`aa6728b`](https://togithub.com/npm/cli/commit/aa6728b1d003f0fc620b074ba0396a3e07f2db6a) [#&redwoodjs#8203;6859](https://togithub.com/npm/cli/pull/6859) `tar@6.2.0` - [`ce9089f`](https://togithub.com/npm/cli/commit/ce9089f604a01297d3d2dd544283696a6297dce5) [#&redwoodjs#8203;6859](https://togithub.com/npm/cli/pull/6859) `npm-package-arg@11.0.1` - [`0a47af5`](https://togithub.com/npm/cli/commit/0a47af509d66071908c7e0bf065dcf2f4c877669) [#&redwoodjs#8203;6859](https://togithub.com/npm/cli/pull/6859) `hosted-git-info@7.0.1` - [`3ebc474`](https://togithub.com/npm/cli/commit/3ebc4744433d906e5c491d183fc077ffe79958cf) [#&redwoodjs#8203;6859](https://togithub.com/npm/cli/pull/6859) `@npmcli/query@3.0.1` [`v7.1.0`](https://togithub.com/npm/cli/blob/HEAD/workspaces/arborist/CHANGELOG.md#710-2023-09-08) [Compare Source](https://togithub.com/npm/cli/compare/v7.0.0...v7.1.0) - [`1c93c44`](https://togithub.com/npm/cli/commit/1c93c4430300e3b3bd2cb5bab327c1732f470bca) [#&redwoodjs#8203;6755](https://togithub.com/npm/cli/pull/6755) Add `--cpu` and `--os` option to override platform specific install ([#&redwoodjs#8203;6755](https://togithub.com/npm/cli/issues/6755)) ([@&redwoodjs#8203;yukukotani](https://togithub.com/yukukotani)) [`v7.0.0`](https://togithub.com/npm/cli/blob/HEAD/workspaces/arborist/CHANGELOG.md#700-2023-08-31) [Compare Source](https://togithub.com/npm/cli/compare/v6.5.0...v7.0.0) - [`fb31c7e`](https://togithub.com/npm/cli/commit/fb31c7e5f00ae39e67f9a5d6b6860c1d839c704b) trigger release process ([@&redwoodjs#8203;lukekarrys](https://togithub.com/lukekarrys)) </details> --- 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://developer.mend.io/github/redwoodjs/redwood). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy44Ny4yIiwidXBkYXRlZEluVmVyIjoiMzcuODcuMiIsInRhcmdldEJyYW5jaCI6Im1haW4ifQ==--> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
@dennemark I took another approach to how we could handle your use case. Let me know what you think! 🙂 See here: #9774 |
Closing this one in favor of #9793 |
Make it possible to specify route param values that need to match. If this is your route: `<Route path="/blog/{year}/{month}/{day}" page={BlogPostPage} name="blogPost" />` And you want to only match posts from 2001 you can now do this: `useMatch('/blog/{year}/{month}/{day}', { routeParams: { year: '2001' } })` This is **finally** a solution to matching route paths. The work started in #7469, but we were never able to come up with an api/dx that we really liked. This PR and #9755 together however provides a solution that we're much more happy with, and that also supports the use case outlined in that original PR. Here's the example from #7469 as it could be solved with the code in this PR ```jsx const Navbar () => { const { project } = useParams() const routePaths = useRoutePaths() const modes = [ { name: "Info", route: routes.info({ project }), match: useMatch(routePaths.info), // using the hook together with routePaths }, { name: "Compare", route: routes.compare({ project, id: "1" }), match: useMatch(useRoutePath('compare')), // alternative to the above }, // ... ] return ( <> {modes.map((x) => <Button as={Link} to={x.route} isActive={x.match} />)} </> ) } ``` And, as described at the top of this description, we can also be more specific than in that example if needed. Like if we only wanted to match a specific project on the "Compare" route we could do this: ```jsx const modes = [ { name: "Info", route: routes.info({ project }), match: useMatch(routePaths.info), }, { name: "Compare against Alpha", route: routes.compare({ project, id: "1" }), match: useMatch(useRoutePath('compare'), { routeParams: { project: 'alpha' } }), }, { name: "Compare against Beta", route: routes.compare({ project, id: "1" }), match: useMatch(useRoutePath('compare'), { routeParams: { project: 'beta' } }), }, // ... ] ``` Here's another example ```jsx <Route path="/{dynamic}/{path}" page={ExamplePage} name="example" /> const exampleRoutePath = useRoutePath('example') // => '/{dynamic}/{path}' const matchOnlyDog = useMatch(exampleRoutePath, { routeParams: { dynamic: 'dog' }}) const matchFullyDynamic = useMatch(exampleRoutePath) ``` In the above example, if the current page url was `https://example.org/dog/fido` then both `matchOnlyDog` and `matchFullyDynamic` would have `match: true`. If the current page instead was `https://example.org/cat/garfield` then only `matchFullyDynamic` would match (This PR replaces #9774)
Adds a property
path
to routes object.Route can still be called via
routes.myRoute({ id: 1 })
giving an exact string like/myRoute/1
. Alternatively we can get the parameter string viaroutes.myRoute.path
:/myRoute/{id: Int}
.Also documents use of routes within useMatch.
Now it is convenient to match a given parametric route: useMatch(routes.myRoute.path).
Alternative would be, to call routes.myRoute() without any parameters, to get the original path. Not sure what is preferable.
The approach has one limitation though. If we have two routes with similar parameters, useMatch will be true for both of them. I added tests, too, but commented them, since they would fail for what we expect.
Hope it suits redwood well!