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.

Link href is not updated when it's dynamic

See original GitHub issue

I’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:closed
  • Created 7 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
fourpixelscommented, Jun 27, 2016

Yeah I forgot about the multiple listeners added 😒 Works like a charm now - thanks!

0reactions
krasimircommented, Jul 16, 2016

#28 merged in.

Read more comments on GitHub >

github_iconTop 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 >

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