# Define routes dynamically based on dependencies

**URL:** <https://discourse.aurelia.io/t/define-routes-dynamically-based-on-dependencies/4705>\
**Category:** vNext\
**Created:** [March 22, 2022, 1:20pm UTC](https://discourse.aurelia.io/t/define-routes-dynamically-based-on-dependencies/4705 "2022-03-22T13:20:17Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![elitastic](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/elitastic/32/1787_2.png) [@elitastic](https://discourse.aurelia.io/u/elitastic)\
**Post date:** [March 22, 2022, 1:20pm UTC](https://discourse.aurelia.io/t/define-routes-dynamically-based-on-dependencies/4705/1 "2022-03-22T13:20:17Z")

</div>

In v1, it was possible to specifiy routes dynamically based on dependencies like:

```auto
@autoinject
export class MyComponent {

	constructor(
		private permissions: Permissions)
	{
	}

    async configureRouter(config: RouterConfiguration, router: Router)
    {
		if (this.permissions.feature1)
        {
            routes.push({
                route: 'route1',
                name: 'route1',
                moduleId: PLATFORM.moduleName('./component1')
            });
        }

        if (this.permissions.feature2)
        {
            routes.push({
                route: 'route2',
                name: 'route2',
                moduleId: PLATFORM.moduleName('./component2')
            });
        }

        config.map(routes);
        config.mapUnknownRoutes(routes[0]);
    }
}

```

Is there still a possibility to do that in Aurelia 2? Creating routes using a static property or by using the route decorator doesn’t seem to work for this scenario?

Thanks for any help.

---

<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:** [March 22, 2022, 3:10pm UTC](https://discourse.aurelia.io/t/define-routes-dynamically-based-on-dependencies/4705/2 "2022-03-22T15:10:26Z")

</div>

AFAIK this kind of conditional route configuration is not supported. However, you can use the routing hooks as described here: [https://docs.aurelia.io/developer-guides/routing/router-hooks#authentication-using-lifecycle-hooks](https://docs.aurelia.io/developer-guides/routing/router-hooks#authentication-using-lifecycle-hooks)

---

<div class="post-metadata">

**Author:** ![elitastic](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/elitastic/32/1787_2.png) [@elitastic](https://discourse.aurelia.io/u/elitastic)\
**Post date:** [March 23, 2022, 8:31am UTC](https://discourse.aurelia.io/t/define-routes-dynamically-based-on-dependencies/4705/3 "2022-03-23T08:31:49Z")

</div>

Thanks for your feedback. Isn’t it a bit too simple if routes can only be defined statically? I can imagine many different scenarios where routes depend on dynamic state, especially in larger business applications.

---

<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:** [March 23, 2022, 10:08am UTC](https://discourse.aurelia.io/t/define-routes-dynamically-based-on-dependencies/4705/4 "2022-03-23T10:08:59Z")

</div>

If your need is simply limited to authorized routes then, as I said before, you can use the hooks. Otherwise, for more dynamism, you may use the [GitHub - jwx/aurelia-direct-router](https://github.com/jwx/aurelia-direct-router).

---

<div class="post-metadata">

**Author:** ![elitastic](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/elitastic/32/1787_2.png) [@elitastic](https://discourse.aurelia.io/u/elitastic)\
**Post date:** [March 24, 2022, 10:42am UTC](https://discourse.aurelia.io/t/define-routes-dynamically-based-on-dependencies/4705/5 "2022-03-24T10:42:07Z")

</div>

OK, thanks for your help!

---

<div class="post-metadata">

**Author:** ![milkshakeuk](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/milkshakeuk/32/1207_2.png) [@milkshakeuk](https://discourse.aurelia.io/u/milkshakeuk)\
**Post date:** [March 28, 2022, 8:23pm UTC](https://discourse.aurelia.io/t/define-routes-dynamically-based-on-dependencies/4705/6 "2022-03-28T20:23:39Z")

</div>

@Sayan751 I thought the direct router was meant to be v2 core again?

---

<div class="post-metadata">

**Author:** ![jwx](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/jwx/32/507_2.png) [@jwx](https://discourse.aurelia.io/u/jwx)\
**Post date:** [March 28, 2022, 10:18pm UTC](https://discourse.aurelia.io/t/define-routes-dynamically-based-on-dependencies/4705/7 "2022-03-28T22:18:56Z")

</div>

It is, it’s just not there yet. I’m working on it and hope to present news soon. (The direct router supports dynamically configuring routes.)

---

<div class="post-metadata">

**Author:** ![elitastic](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/elitastic/32/1787_2.png) [@elitastic](https://discourse.aurelia.io/u/elitastic)\
**Post date:** [May 30, 2022, 11:08am UTC](https://discourse.aurelia.io/t/define-routes-dynamically-based-on-dependencies/4705/8 "2022-05-30T11:08:29Z")

</div>

Looks like this is what I need! 🙂

> <https://github.com/aurelia/aurelia/pull/1439>
>
> \<!---
> Thanks for filing a pull request 😄 ! Before you submit, please read the f…ollowing:
> 
> Search open/closed issues before submitting since someone might have pushed the same thing before!
> \--\>
> 
> \# Pull Request
> 
> \## 📖 Description
> 
> \<!---
> Provide some background and a description of your work.
> \--\>
> 
> This PR adds support for an instance (non-static) hook to configure routes. This can be thought as the parallel of \`configureRouter\` in v1. The difference is in the signature and obviously the schema of the routing configuration object.
> 
> \`\`\`typescript
> getRouteConfig(parentDefinition: RouteDefinition | null, routeNode: RouteNode | null): IRouteConfig;
> \`\`\`
> 
> This means that the following two routing configurations are synonymous.
> 
> \`\`\`typescript
> @route({
> routes: \[
> { path: 'c1', component: C1 },
> { path: 'c2', component: C2 },
> \],
> })
> \`\`\`
> 
> \`\`\`typescript
> public getRouteConfig(def: RouteDefinition | null, node: RouteNode | null): IRouteConfig {
> return {
> routes: \[
> { path: 'c1', component: C1 },
> { path: 'c2', component: C2 },
> \],
> };
> }
> \`\`\`
> 
> Obviously, the non-static infra adds the flexibility of dynamically configuring the routing table, like v1.
> 
> Additionally, it sets the default transition plan to \`replace\` as the \`invoke-lifecycles\` plan often causes unexpected surprises.
> 
> \### 🎫 Issues
> 
> \<!---
> \* List and link relevant issues here.
> \--\>
> 
> \## 👩‍💻 Reviewer Notes
> 
> \<!---
> Provide some notes for reviewers to help them provide targeted feedback.
> \--\>
> 
> Note that the \`@route\` decorator can be deprecated. However, we might need to think it over, whether we want to do that. The decorator still offers less verbosity. And sometimes, if the routing needs are not that critical, static routes might work as well. I am not saying we should not remove that, because removing will also result in removing some related infra. I am suggesting to postpone the removal of the decorator to a later point, after the things are discussed.
> 
> \## 📑 Test Plan
> 
> \<!---
> Please provide a summary of the tests affected by this work and any unique strategies employed in testing the features/fixes.
> \--\>
> 
> \## ⏭ Next Steps
> 
> \<!---
> If there is relevant follow-up work to this PR, please list any existing issues or provide brief descriptions of what you would like to do next.
> \--\>
> 
> \<!--
> Love Aurelia? Please consider supporting our collective:
> 👉 https://opencollective.com/aurelia
> \--\>
