ember.js icon indicating copy to clipboard operation
ember.js copied to clipboard

[3.15] Router currentURL Not Set

Open jherdman opened this issue 5 years ago • 9 comments

In Ember 3.12 the following code would report the current URL of the application:

class MyRoute extends Route {
  @service router;

  activate() {
    super.activate(...arguments);
    console.log('here you are:', this.router.currentURL);
  }
}

In Ember 3.15 this will always report null. This is expected to work per RouterService#currentURL. Cursory investigation shows that both currentRouteName nor currentRoute are set either.

Curiously the RouterService reports the correct URL when used within a Component.

Sample application: https://github.com/jherdman/where-art-thou. Note that the console will always log a null URL, whereas the component correctly reports the URL.

jherdman avatar Jan 07 '20 14:01 jherdman

I'd guess, if you test this, that it works at other points in a route's lifecycle – e.g. willTransition or didTransition, or actions triggered from a controller via this.send; have you checked that? This is definitely breakage, but it's also (unfortunately) underspecified in the RFC, and therefore there may not be coverage on it in the implementation.

Aside: I can't find any indication that EmberRouter#activate is public API? Do you mean Route#activate instead?

class MyRoute extends Route {
  @service router;

  activate() {
    super.activate(...arguments);
    console.log('here you are:', this.router.currentURL);
  }
}

chriskrycho avatar Jan 07 '20 22:01 chriskrycho

Hi Chris! Thank you for the reply.

For completeness I've added a didTransition action too, but it sadly shows the same bug. As far as I can tell, this does not work at all on routes.

Aside: I can't find any indication that EmberRouter#activate is public API? Do you mean Route#activate instead?

I think there was a bit of misunderstanding, I am indeed testing Route#activate. I've amended the snippet in my issue. The linked app has a fully functioning demo of the broken behaviour.

jherdman avatar Jan 08 '20 15:01 jherdman

That's pretty weird indeed. 😬 I won't have a chance to poke at this till next week at earliest, but that's apt to hit our app as well as we upgrade, so at least investigating further is reasonably high priority.

chriskrycho avatar Jan 10 '20 20:01 chriskrycho

https://github.com/emberjs/ember.js/issues/18585 seems related.

locks avatar Jan 15 '20 15:01 locks

I'm noticing this is affecting test helpers in 3.16 too.

jherdman avatar Mar 16 '20 21:03 jherdman

Should we mark this issue as Router Bugs given doc has been updated to mention update timing?

xg-wang avatar Mar 17 '20 02:03 xg-wang

I'm not really sure. I noticed today that the currentURL test help is reporting null sometimes whereas it didn't in the past. I haven't had much time to dig into latest development.

jherdman avatar Mar 17 '20 04:03 jherdman

I just hit this issue too, I asked about it first in discord, but then found this issue.

Specifically in the didTransition hook. Is anyone aware of a work around?

Edit: Can use routeDidChange hook to workaround this, as described by @jelhan here:

  init() {
   super.init(...arguments); 
   this.router.on('routeDidChange', () => {
     console.log('routeDidChange', this.router.currentRoute, this.router.currentRouteName, this.router.currentURL);
   });
  }

lougreenwood avatar Apr 05 '20 16:04 lougreenwood

Ya, I guess we should consider deprecating didTransition now that the router service alternatives have been around for a while...

:thinking:

rwjblue avatar May 23 '20 20:05 rwjblue