-
-
Notifications
You must be signed in to change notification settings - Fork 408
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
Arbitrary Query Params #715
Arbitrary Query Params #715
Conversation
How about sticky query params? Wouldn't exist in this mode, I assume, but what would a user-space implementation look like? Same question for "default query params". When doing refresh on a route, would the query params stay around? (and would the route model hook receive them) |
Correct! I'm thinking something like this: // this could also live on some service
const stickyQPs = {};
export default MyRoute extends Route {
@service router;
async beforeModel({ to: { queryParams }}) {
if (!stickyQPs.category) {
stickyQPs.category = queryParams.category;
}
if (!queryParams.category) {
this.router.transitionTo({ queryParams: { category: 'default value' }});
}
}
export default MyRoute extends Route {
@service router;
async beforeModel({ to: { queryParams }}) {
if (!queryParams.category) {
this.router.transitionTo({ queryParams: { category: 'default value' }});
}
}
yup, the I should add these examples to here: #712 |
Some followup questions: With sticky, how would that work with links? A With default query params, you'd get the link Also, triggering I don't really know what I think of the overall proposal (I'm neither for nor against yet), but wanted to highlight some possible problems. I agree with you that decoupling query params from controllers would be great. It's awesome that we're thinking about different ways of getting rid of them. |
I think you'd want to make your own Link component and store the sticky data in a service and have your custom link component reference that service.
yeah, I don't think we should ever have QP state that isn't represented in the URL, so what you propose is totally valid, and I think the behavior absolutely should be left up to folks to decide for themselves in their apps :D
the URL, including query params, are the state though -- but that's just a thing with my proposed whipped up implementation. If someone didn't want to transition, that's fine, too.
Yeah, this is really the last thing we need to stop using controllers, allowing for better composition patterns (I know this is subjective, but we can talk about that on Discord :p ). |
|
||
```diff | ||
export default class ArticlesController extends Controller { | ||
- queryParams = ['category']; |
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.
There is some maybe undefined behavior around not having this specified atm.
For example, this.router.currentRoute.queryParams
is still updated with transitions, but all QPs are stripped from the URL.
This RFC, to put it simply, makes all things equal:
- transitionTo matches the URL and router service state
- does not set properties on controllers
That's it.
The first statement is not true today, and is a huge source of weird
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.
Here is a demonstration of what happens today when the query params array is omitted: https://ember-twiddle.com/7e472191b3f5021433b8552158a4379e?openFiles=routes.articles%5C.js%2C&route=%2Farticles
(this demo mostly demonstrates how to manage your own sticky params with Ember 3.18, but there is weird router behavior demonstrated, too)
…-- the sticky query params example is more applicable to emberjs#715
Coming from a more recent Ruby on Rails background, this change makes sense to me. You have access to all However, I would would also like to ask/suggest if we could find a corresponding to Rail’s strong parameters (either by building something into the framework or suggest a good example of how to deny/allowlist parameters). Maybe together with #712, we could provide a documentation example of how to derive safe parameters (i.e. filtering out random parameters that we don’t want to pass on), something like this: import Component from '@glimmer/component';
import { action } from '@ember/object';
import { inject as service } from '@ember/service';
export default class MyDeeplyNestedComponent extends Component {
@service router;
get params() {
const { page, sort, category } = this.router.currentRoute.queryParams;
return { page, sort, category };
}
} |
@gnclmorais the threat model for a single page app is somewhat different from Rails, so I don't know if something like strong params would necessarily make sense here. Common vectors are XSS, CSRF and a few others. Sure, one can certainly make up a scenario where non-sanitized urls could allow an attacker to e.g. trick another user into doing something harmful, but params are overall much less sensitive on the front-end. |
You’re right @sandstrom, it’s not the same on the frontend side — I was providing a parallel with Rails mostly as a documentation example, a way we can put this PR and #712 together to provide more clarity. Removing the |
@gnclmorais I agree regarding documentation, self-documenting code would definitely be useful + agree that whitelisting params is a good idea (but not primarily for security). |
|
||
// if transitioning to a route with explicit query params, | ||
// update the query param cache | ||
if (stickyQPs.category && category !== queryParams.category) { |
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.
Where did this stickyQPs
comes from? Is this a typo?
@NullVoxPopuli where does this stand? |
It's something I think we absolutely need as a framework, but the timing of the concept of arbitrary query params may fit better with whatever overhauls the routing system may get with/post polaris. Would be good to talk to / hear from core about what's planned around that. My primary belief around QPs is that opt-in by default is backwards, and this RFC reverses that. |
Thanks for spending the time writing this RFC! Given the significant number of concerns around routing, we’re going to be completely revamping the router as part of Polaris. This RFC won’t directly apply to the new router so we’re going to move this PR to FCP to Close. But don’t worry, your work here was not in vain! We will be keeping track of this RFC and others like it to make sure that the concerns it aims to address will be covered in the new router. Your work here does a significant service to us in this regard! |
rendered
Side-effecting goals by not defaulting to sticky params:
Example of what happens today when the
queryParams
array is omitted:(Twiddle is Ember 3.18)