-
Notifications
You must be signed in to change notification settings - Fork 10.2k
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 IgnoreQueryString parameter to NavLink #40990
Add IgnoreQueryString parameter to NavLink #40990
Conversation
This commit adds the `IgnoreQueryString` parameter, allowing the user to have routes match their URLs regardless of whether query parameters are set or not. Co-authored-by: James Yeung <[email protected]> Co-authored-by: Safia Abdalla <[email protected]>
If a route includes trailing slashes and query parameters, given `Match` is set to `NavLinkMatch.All` and `IgnoreQueryString` is set to `true`, the base route (without trailing slash and query string) should be matched.
Thanks for your PR, @igotinfected. |
I think this PR would be very useful. I hope it goes through. |
/// <summary> | ||
/// Gets or sets a flag to indicate whether route matching should ignore the query string. | ||
/// </summary> | ||
[Parameter] | ||
public bool IgnoreQueryString { get; set; } |
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.
Hmm, I wonder if this behavior could potentially be treated as a subset of #18163?
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.
@TanayParikh Reading through that issue made me realise this IgnoreQueryString
implementation would also ignore the Fragment
part of the URI, if specified after a query string, and in other words, this implementation does not ignore fragments if they are the only extension to the base path.
I'd be interested in taking a look at the ShouldMatch
approach if that's the direction we want to go in, just let me know
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 don't think we need a new flag for this. This is just a bug/oversight in existing code
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 don't think we need a new flag for this
Are you referring to the whole IgnoreQueryString
flag, or to some other hypothetical new flag related to @TanayParikh's comment about the hash part of the URL?
If you mean IgnoreQueryString
itself, I agree it would be better if we were ignoring the querystring by default. However that would be a breaking change we would need to weigh up carefully.
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 don't think matching on the query string would have offered a good behavior before. Query strings are not ordered, so the chances of matching on the query string would have been incredibly narrow.
I think we can just make the change and make an announcement about it if we deeply care about it.
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 agree about the default, however there are clear cases where someone might have wanted it to match on querystrings and might in fact be depending on that behavior now. For example, if you have:
@page "/products"
@code {
[Parameter]
[SupplyParameterFromQuery(Name = "category_id")]
public int CategoryId { get; set; }
}
... then you might also have navigation links like:
<NavLink href="products?category_id=1">Dogs</NavLink>
<NavLink href="products?category_id=2">Cats</NavLink>
<NavLink href="products?category_id=3">Fish</NavLink>
... and expect those links to light up to show the current page.
It's not the most common pattern, but it's a valid and believable one so we should be prepared for making a clear announcement and treating it as a breaking change if we're going to change this.
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.
Yep, I'm fine if we do that.
The alternative is to properly support query string parameters, but that is much more work.
I want to avoid the situation where we have a behavior that "half" works and I think in this case it is simpler to not support it at all vs to add full support for it.
Add
IgnoreQueryString
parameter toNavLink
Description
I've repurposed a stale PR (#32168) that gives users more control on
NavLink
matches when query strings are present. Do note that while writing the missing E2E tests, I noticed that the original implementation would not cover the case where a query string followed a trailing slash (i.e./abc/?value=123
). This is now covered by trimming a trailing slash if it is present when the user asks for the query string to be ignored.Fixes #31312