# RedirectTo and canLoad of lifecycleHooks

**URL:** <https://discourse.aurelia.io/t/redirectto-and-canload-of-lifecyclehooks/5055>\
**Category:** vNext\
**Created:** [January 17, 2023, 8:38pm UTC](https://discourse.aurelia.io/t/redirectto-and-canload-of-lifecyclehooks/5055 "2023-01-17T20:38:16Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![DanieleRosada](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/danielerosada/32/1981_2.png) [@DanieleRosada](https://discourse.aurelia.io/u/DanieleRosada)\
**Post date:** [January 17, 2023, 8:38pm UTC](https://discourse.aurelia.io/t/redirectto-and-canload-of-lifecyclehooks/5055/1 "2023-01-17T20:38:16Z")

</div>

RedirectTo not working with canLoad of @lifecycleHooks. If you comment canLoad function, redirectTo works. Could someone help me, please?

> **[canload hook and redirectTo - StackBlitz](https://stackblitz.com/edit/au2-conventions-t5phkt)**
>
> Run Aurelia application with conventions

---

<div class="post-metadata">

**Author:** ![DanieleRosada](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/danielerosada/32/1981_2.png) [@DanieleRosada](https://discourse.aurelia.io/u/DanieleRosada)\
**Post date:** [February 10, 2023, 1:34pm UTC](https://discourse.aurelia.io/t/redirectto-and-canload-of-lifecyclehooks/5055/2 "2023-02-10T13:34:55Z")

</div>

@Sayan751 sorry for the tag. The problem is async canLoad, could you see this?

---

<div class="post-metadata">

**Author:** ![Sayan751](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/sayan751/32/1861_2.png) [@Sayan751](https://discourse.aurelia.io/u/Sayan751)\
**Post date:** [February 11, 2023, 6:31pm UTC](https://discourse.aurelia.io/t/redirectto-and-canload-of-lifecyclehooks/5055/3 "2023-02-11T18:31:17Z")

</div>

Hi @DanieleRosada! Can you please elaborate a little bit more, what exactly isn’t working? I have forked your example, and it seems to be working.

 ![image](https://canada1.discourse-cdn.com/flex027/uploads/aurelia/original/2X/6/6d3ae5fd738ba0b69a1381d642cd7918e0e1340e.png)

---

<div class="post-metadata">

**Author:** ![DanieleRosada](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/danielerosada/32/1981_2.png) [@DanieleRosada](https://discourse.aurelia.io/u/DanieleRosada)\
**Post date:** [February 12, 2023, 3:33pm UTC](https://discourse.aurelia.io/t/redirectto-and-canload-of-lifecyclehooks/5055/4 "2023-02-12T15:33:57Z")

</div>

I see two behavioral patterns in “async canLoad" and "canLoad”, a function of lifecyclehook component. In the first case, redirectTo is in the middle state and by that I need to press Dashboard link to see the child viewport. In the second case the redirecting is automatic.  
I need to understand if that is the right way to use redirectTo or if it is a bug.

---

<div class="post-metadata">

**Author:** ![Sayan751](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/sayan751/32/1861_2.png) [@Sayan751](https://discourse.aurelia.io/u/Sayan751)\
**Post date:** [February 12, 2023, 5:28pm UTC](https://discourse.aurelia.io/t/redirectto-and-canload-of-lifecyclehooks/5055/5 "2023-02-12T17:28:30Z")

</div>

Sorry. I have missed the obvious point.

I have tried your example with `@aurelia/router-lite`: [discourse 5055 - StackBlitz](https://stackblitz.com/edit/discourse-5055?file=src/elements/auth-hook.ts). It seems to be working as expected. If your routing needs are limited to configured routing or if your project is not yet coupled with the `@aurelia/router`, you may once give the `@aurelia/router-lite` a try.

As for why it is not working for `@aurelia/router`, I am not sure. @jwx maybe something is missed in the example?

---

<div class="post-metadata">

**Author:** ![DanieleRosada](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/danielerosada/32/1981_2.png) [@DanieleRosada](https://discourse.aurelia.io/u/DanieleRosada)\
**Post date:** [February 19, 2023, 6:32pm UTC](https://discourse.aurelia.io/t/redirectto-and-canload-of-lifecyclehooks/5055/6 "2023-02-19T18:32:16Z")

</div>

I’m back after one week of trial with @aurelia/router-lite. It works well, nice job.  
Here’s a list of doubts I still have:

BUGS

- history.back() not always working (DEMO: [bug history.back() - StackBlitz](https://stackblitz.com/edit/discourse-5055-omx9ww))
- field isActive in @IRouteContext isn’t true when I use redirectTo

QUESTIONS

- Why routes like this {path: ‘’, redirectTo: ‘home’} are inside @IRouteContext.navigationModel.routes?
- Why, if I use @IRouter router.load() in the same class of routes defined, do I need to pass the current context?

---

<div class="post-metadata">

**Author:** ![Sayan751](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/sayan751/32/1861_2.png) [@Sayan751](https://discourse.aurelia.io/u/Sayan751)\
**Post date:** [February 19, 2023, 7:29pm UTC](https://discourse.aurelia.io/t/redirectto-and-canload-of-lifecyclehooks/5055/7 "2023-02-19T19:29:21Z")

</div>

Thank you for trying out router-lite!

> - history.back() not always working (DEMO: [bug history.back() - StackBlitz](https://stackblitz.com/edit/discourse-5055-omx9ww))

The bug is already been fixed, and the fix is available under the `dev`tag. If you remove your package-lock.json, save and reload (this will regenerate the lock file), you can see it to be working. Here is a working example: [bug history.back() (forked) - StackBlitz](https://stackblitz.com/edit/discourse-5055-kkgfbo?file=package.json).

> - field isActive in @IRouteContext isn’t true when I use redirectTo

Do you mean the `@IRouteContext.navigationModel.routes[i].isActive`? If yes, then I have probably missed something. Thank you, reporting this. I will take a look. A temporary workaround might be to use an array of paths instead of redirectTo, depending on your use-case.

> - Why routes like this {path: ‘’, redirectTo: ‘home’} are inside @IRouteContext.navigationModel.routes?

This is like the `NavModel` (available via `router.navigation`) in Au1 ([Au1 docs](https://aurelia.io/docs/routing/configuration)). The [Au2 docs](https://docs.aurelia.io/router-lite/navigation-model) explains this.

> - Why, if I use @IRouter router.load() in the same class of routes defined, do I need to pass the current context?

This is because the `IRouter` instance in router-lite is singleton. And when you don’t specify a routing context, the default is always the root context. This is [documented here](https://docs.aurelia.io/router-lite/navigating#using-the-router-api).

---

<div class="post-metadata">

**Author:** ![DanieleRosada](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/danielerosada/32/1981_2.png) [@DanieleRosada](https://discourse.aurelia.io/u/DanieleRosada)\
**Post date:** [February 19, 2023, 9:29pm UTC](https://discourse.aurelia.io/t/redirectto-and-canload-of-lifecyclehooks/5055/8 "2023-02-19T21:29:38Z")

</div>

> The bug is already been fixed, and the fix is available under the `dev`tag. If you remove your package-lock.json, save and reload (this will regenerate the lock file), you can see it to be working. Here is a working example: [bug history.back() (forked) - StackBlitz](https://stackblitz.com/edit/discourse-5055-kkgfbo?file=package.json).

You’re right, sorry.

> Do you mean the `@IRouteContext.navigationModel.routes[i].isActive` ? If yes, then I have probably missed something. Thank you, reporting this. I will take a look. A temporary workaround might be to use an array of paths instead of redirectTo, depending on your use-case.

Yes I mean this.

Thanks for the documentation links

---

<div class="post-metadata">

**Author:** ![Sayan751](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/sayan751/32/1861_2.png) [@Sayan751](https://discourse.aurelia.io/u/Sayan751)\
**Post date:** [February 23, 2023, 7:27pm UTC](https://discourse.aurelia.io/t/redirectto-and-canload-of-lifecyclehooks/5055/9 "2023-02-23T19:27:50Z")

</div>

The `isActive` issue with the `redirectTo` should be fixed now. The fix is available under the `dev` tag. For more information, you may refer the [PR](https://github.com/aurelia/aurelia/pull/1681).
