question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Router: all routerLink anchors include/inherit query string params from current route, but should not.

See original GitHub issue

I’m submitting a …

[X] bug report
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior When entering a route whose component includes [routerLink] directives in its template, all router links are rendered with the query string parameter of the route you entered. This should not be the case.

Expected/desired behavior Router links should only include query string parameters provided to them in the template (which AFAIK is not possible right now). They should not inherit the query string params from the current route responsible for displaying those router links.

Reproduction of the problem You can see the issue here:

http://plnkr.co/edit/PPysE0X030pSF5eUtXb5?p=preview

Enter the plunker and note that the “item” links do not include any query string params. Next, click on one of the links. In that route you’ll find a link that navigates back, but adds a query string param to the route. Click on it. Now all item router links include the query string, but they should not.

What is the motivation / use case for changing the behavior? It affects pretty much any app using query strings, really. One might have for example a login route which might include a query string indicating where to redirect to after the login. If there are routerLink directives on that login page, then all links will carry on that query string which is incorrect.

Please tell us about your environment:

  • Angular version: 2.0.0-rc.4
  • Router version: 3.0.0-beta.2
  • Browser: all
  • Language: all

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
nosachamoscommented, Jul 9, 2016

@vsavkin I believe the assessment made on issue https://github.com/angular/angular/issues/9573 , similar to this one, that “Query params are meant to be global and shared, and thus persist.” is incorrect.

From RFC 3986 (https://tools.ietf.org/html/rfc3986#section-3.4):

The query component contains non-hierarchical data that, along with data in the path component Section 3.3), serves to identify a resource within the scope of the URI's scheme and naming authority (if any).

Which means the query part of the URL contains data that serves to identify a resource, and therefore should not apply to other resources which are linked by routerLink.

Also, same goes for the fragment (https://tools.ietf.org/html/rfc3986#section-3.5):

The fragment identifier component of a URI allows indirect identification of a secondary resource by reference to a primary resource and additional identifying information.

Again, tied to the primary resource which is loaded by the “current” route. The routerLink directive links to other resources.

Besides, if that was the case, browsers would carry query string parameters from the current URL to all anchors in the page just like they do by default with the scheme, host and port, or leading path when dealing with relative URLs. The only case where browsers won’t do this is when the scheme, host, port, etc are explicitly defined in the anchor URL, so it overrides the default behavior, which is fine.

It would be, at best, acceptable to carry over query and fragments like that for routerLinks that link to the same resource (and therefore same route) as the current one bearing the given data. IMO such special case would just make it all more confusing. Best would be not to carry them over at all, and allow the user to set those in the router link if desired.

Thoughts?

1reaction
slubowskycommented, Jul 8, 2016

This is coming up again and again. Very unintuitive. See for example: https://github.com/angular/angular/issues/9573 https://github.com/angular/angular/issues/9508

Seems to me the default behavior should be not to attach query params and I should be able to buy in to get query params by setting some property. (Obviously sometimes this is a good thing, it just shouldn’t be done by default)

What makes it clear to me that current behavior is “wrong” is that currently pages will behave differently depending on how user navigated to them. Navigate from app root, get to some page and outer router links created on app start will not have query param attached. Reload page (very common - say from a bookmark) , page looks the same and has identical URL but outer router links now have query params and may do something different when clicked.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to pass query parameters with a routerLink - Stack Overflow
To pass query parameters to this.router.navigate(...) use let navigationExtras: NavigationExtras = { queryParams: { 'session_id': sessionId }, fragment: ...
Read more >
How To Use Query Parameters with Angular Router
In this article, we will reference an example of an application that displays a list of products to understand how to use query...
Read more >
React Router Cheatsheet | Codecademy
React Router's useParams() hook can be used by a component rendered by a <Route> with a dynamic path to get the names and...
Read more >
Router - Yew
Yew's official router. ... The router will consider the route as unmatched and try to render the not found ... Obtaining query parameters...
Read more >
Passing Query Parameters to Route - Angular 13+ - YouTube
We learned about route parameters in the last two lectures. In this lecture let's learn what is a query parameter, how to pass...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found