Link href is not updated when it's dynamic
See original GitHub issueI’m using Navigo with dynamic links - I have a simple href link with data-navigo
attribute. When I click it, everything works out well, I get new data from the server.
But then I update the href attribute, using something like:
$('#link').attr('href', '/new/value');
Unfortunately this does not work properly. I first tried calling updatePageLinks
, which is not documented but it’s available as a function. It kind of works, but it attaches a lot of listeners to the very same link, because old ones are not cleared. I guess it just fires up a lot of redirects and I end up on the right one, I don’t know (didn’t investigate it too much).
I think the location property should be checked inside the click listener, instead before that:
updatePageLinks: function updatePageLinks() {
var _this = this;
if (typeof document === 'undefined') return;
this._findLinks().forEach(function (link) {
var location = link.getAttribute('href'); // do NOT use this one, as it's set only once
link.addEventListener('click', function (e) {
if (!_this._destroyed) {
e.preventDefault();
//_this.navigate(clean(location)); // don't use pre-saved location
// get one each time the link is clicked
// it's slower, but more robust and works for dynamic href changes
// I found out that pathname is the actual href
// as href gets transformed to the real address
// ex: href="/test" -> e.currentTarget.href = "www.example.com/test"
_this.navigate(clean(e.currentTarget.pathname));
}
});
});
}
I can make PR if you find this helpful 😃
Issue Analytics
- State:
- Created 7 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
AngularJs ng-href dynamic link with scope not updating/working
The reason it's not updating is because your click callback is outside of the Angular $digest loop. But, you shouldn't be accessing the...
Read more >Dynamic basename does not update links when it is changed
In the codesandbox link you can try clicking on some links and see that the url has '/1' properly prepended. But, if you...
Read more >How to Render <a> with Optional href in React | Pluralsight
In this guide, you'll learn how to render an anchor element in your React app with optional href based on specific conditions.
Read more >Removing “href” from Next.js Dynamic Routes | by Josiah Dudzik
The solution is to eliminate the need to manually define href on every link by automatically deriving it based on the provided URL....
Read more >Create Dynamic Links on iOS - Firebase
In the Firebase console, open the Dynamic Links section. If you have not already accepted the terms of service and set a URI...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Yeah I forgot about the multiple listeners added 😒 Works like a charm now - thanks!
#28 merged in.