Skip to content
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

Closed
wants to merge 3 commits into from
Closed

Add IgnoreQueryString parameter to NavLink #40990

wants to merge 3 commits into from

Conversation

igotinfected
Copy link

Add IgnoreQueryString parameter to NavLink

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

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

igotinfected and others added 3 commits April 1, 2022 01:32
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.
@igotinfected igotinfected requested a review from a team as a code owner April 1, 2022 03:02
@ghost ghost added area-blazor Includes: Blazor, Razor Components community-contribution Indicates that the PR has been added by a community member labels Apr 1, 2022
@dnfadmin
Copy link

dnfadmin commented Apr 1, 2022

CLA assistant check
All CLA requirements met.

@mkArtakMSFT
Copy link
Member

Thanks for your PR, @igotinfected.
@javiercn can you please review this? Thanks!

@mfluehr
Copy link
Contributor

mfluehr commented Jun 28, 2022

I think this PR would be very useful. I hope it goes through.

Comment on lines +47 to +51
/// <summary>
/// Gets or sets a flag to indicate whether route matching should ignore the query string.
/// </summary>
[Parameter]
public bool IgnoreQueryString { get; set; }
Copy link
Contributor

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?

Copy link
Author

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 ☺️

Copy link
Member

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

Copy link
Member

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.

Copy link
Member

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.

Copy link
Member

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.

Copy link
Member

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.

@igotinfected igotinfected closed this by deleting the head repository Dec 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

When using a querystring with Blazor apps the the default NavItem is not selected.
7 participants