ember.js
ember.js copied to clipboard
[3.15] Router currentURL Not Set
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.
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);
}
}
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.
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.
https://github.com/emberjs/ember.js/issues/18585 seems related.
I'm noticing this is affecting test helpers in 3.16 too.
Should we mark this issue as Router Bugs given doc has been updated to mention update timing?
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.
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);
});
}
Ya, I guess we should consider deprecating didTransition now that the router service alternatives have been around for a while...
:thinking: