# Plugin with catch-all wildcard route map

**URL:** https://meta.discourse.org/t/plugin-with-catch-all-wildcard-route-map/100348
**Category:** Development
**Created:** [October 24, 2018, 12:12pm UTC](https://meta.discourse.org/t/plugin-with-catch-all-wildcard-route-map/100348 "2018-10-24T12:12:09Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![kleinfreund](https://avatars.discourse-cdn.com/v4/letter/k/a6a055/32.png) [@kleinfreund](https://meta.discourse.org/u/kleinfreund)
#### Post date: [October 24, 2018, 12:12pm UTC](https://meta.discourse.org/t/plugin-with-catch-all-wildcard-route-map/100348/1 "2018-10-24T12:12:09Z")

</div>

Discourse has a catch-all route using a wildcard path.

**`app/assets/javascripts/discourse/mapping-router.js.es6`** :

```js
this.route("unknown", { path: "*path" });

```

As far as I can tell, this stops plugins from having the same catch-all rule because Discourse’s rule is executed later, thus taking precedence. Atleast when commenting out Discourse’s rule temporarily, the following works as expected:

**`plugins/plugin/assets/javascripts/discourse/plugin-name-route-map.js.es6`** :

```js
this.route('main-route', { path: '*wildcard' });

```

Is there a way to have a catch-all route for a plugin with only one route?

* * *

**Attempt 1** :

> **Nested routes.**
>
> **`plugins/plugin/assets/javascripts/discourse/plugin-name-route-map.js.es6`** :
> 
> ```js
> export default function () {
> // Matches single-segment paths.
> this.route('main-route', { path: '/:path' }, function () {
> // Optionally, matches subsequent segments.
> this.route('sub-route', { path: '*wildcard_path' });
> });
> }
> 
> ```
> 
> This has the major issue that requests matching `/:path` will use the route `main-route.index` whereas requests matching `/:path/*wildcard` will use the route `main-route.sub-route`. I need all requests to go through `main-route.index`.

* * *

**Attempt 2** :

> **Multiple paths, same routes.**
>
> (This seems to have worked in older Ember version, but does no longer.)
> 
> **`plugins/plugin/assets/javascripts/discourse/plugin-name-route-map.js.es6`** :
> 
> ```js
> export default function () {
> // Matches single-segment paths.
> this.route('main-route', { path: '/:path' });
> 
> // Doesn’t match multi-segment paths.
> this.route('main-route', { path: '/:path/*wildcard_path' });
> }
> 
> ```
> 
> or
> 
> **`plugins/plugin/assets/javascripts/discourse/plugin-name-route-map.js.es6`** :
> 
> ```js
> export default function () {
> // Matches multi-segment paths.
> this.route('main-route', { path: '/:path/*wildcard_path' });
> 
> // Doesn’t match single-segment paths.
> this.route('main-route', { path: '/:path' });
> }
> 
> ```

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [October 30, 2018, 6:12am UTC](https://meta.discourse.org/t/plugin-with-catch-all-wildcard-route-map/100348/2 "2018-10-30T06:12:56Z")

</div>

> [@kleinfreund](#):
>
> Is there a way to have a catch-all route for a plugin with only one route?

I am not sure about this, but maybe @eviltrout can have a quick look.

---

<div class="post-metadata">

### Author: ![eviltrout](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eviltrout/32/5275_2.png) [@eviltrout](https://meta.discourse.org/u/eviltrout)
#### Post date: [October 30, 2018, 1:15pm UTC](https://meta.discourse.org/t/plugin-with-catch-all-wildcard-route-map/100348/3 "2018-10-30T13:15:15Z")

</div>

I don’t think there’s a way to do this. Is there a reason you can’t define all the routes you’re using? I don’t see the downside.

---

<div class="post-metadata">

### Author: ![kleinfreund](https://avatars.discourse-cdn.com/v4/letter/k/a6a055/32.png) [@kleinfreund](https://meta.discourse.org/u/kleinfreund)
#### Post date: [October 30, 2018, 1:35pm UTC](https://meta.discourse.org/t/plugin-with-catch-all-wildcard-route-map/100348/4 "2018-10-30T13:35:48Z")

</div>

The paths I want to direct to the same route are not known before. They’re created at run time.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [October 30, 2018, 1:50pm UTC](https://meta.discourse.org/t/plugin-with-catch-all-wildcard-route-map/100348/5 "2018-10-30T13:50:34Z")

</div>

Can you not use a single route and put the stuff that you want in the route into the load?

---

<div class="post-metadata">

### Author: ![eviltrout](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eviltrout/32/5275_2.png) [@eviltrout](https://meta.discourse.org/u/eviltrout)
#### Post date: [October 30, 2018, 4:22pm UTC](https://meta.discourse.org/t/plugin-with-catch-all-wildcard-route-map/100348/6 "2018-10-30T16:22:41Z")

</div>

Technically, all routes are defined at run time (that’s how a route map works) but I assume you mean after the app has loaded?

Could you not use a dynamic segment for your case? If not, could you be more specific about what you are trying to achieve?

---

<div class="post-metadata">

### Author: ![kleinfreund](https://avatars.discourse-cdn.com/v4/letter/k/a6a055/32.png) [@kleinfreund](https://meta.discourse.org/u/kleinfreund)
#### Post date: [October 30, 2018, 4:47pm UTC](https://meta.discourse.org/t/plugin-with-catch-all-wildcard-route-map/100348/7 "2018-10-30T16:47:44Z")

</div>

I’m working on a plugin which works as a kind of reverse proxy. It allows users to specify routes which upon requesting are forwarded to another URL. The goal is to allow arbitrary web applications to be served within a Discourse instance.

For this, I essentially need to handle all routes that were defined by the plugin users. Therefor, I thought, it’s necessary to catch all requests that aren’t handled by Discourse. In a nutshell, I have unknown paths (they might have multiple segments, query parameters, etc.) that I all want to use the same route for. The route then makes an AJAX call in order to forward the request and retrieve the web application’s content.

I do realize that this is a complex plugin and use case.

* * *

So far, I wasn’t able to make this work with any combination of dynamic path segments and wildcard path segments I tried.

---

<div class="post-metadata">

### Author: ![eviltrout](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eviltrout/32/5275_2.png) [@eviltrout](https://meta.discourse.org/u/eviltrout)
#### Post date: [October 30, 2018, 5:40pm UTC](https://meta.discourse.org/t/plugin-with-catch-all-wildcard-route-map/100348/8 "2018-10-30T17:40:36Z")

</div>

What you are suggesting is quite complicated unfortunately. I suspect you will have to play with Ember’s routing internals to make it work.

---

<div class="post-metadata">

### Author: ![kleinfreund](https://avatars.discourse-cdn.com/v4/letter/k/a6a055/32.png) [@kleinfreund](https://meta.discourse.org/u/kleinfreund)
#### Post date: [November 1, 2018, 9:22am UTC](https://meta.discourse.org/t/plugin-with-catch-all-wildcard-route-map/100348/9 "2018-11-01T09:22:10Z")

</div>

_(via [Override existing catch-all wildcard route - #2 by ef4 - Routing - Ember.JS](https://discuss.emberjs.com/t/override-existing-catch-all-wildcard-route/15717/2))_

The following allows a plugin to have a sort of catch-all style route:

**`plugins/plugin/assets/javascripts/discourse/plugin-name-route-map.js.es6`** :

```js
export default function () {
  this.route('main-route.single-segment', { path: '/:path' });
  this.route('main-route.multi-segment', { path: '/:path/*wildcard' });
}

```

**`plugins/plugin/assets/javascripts/discourse/routes/main-route-single-segment.js.es6`** :

```js
export { default } from './main-route';

```

**`plugins/plugin/assets/javascripts/discourse/routes/main-route-multi-segment.js.es6`** :

```js
export { default } from './main-route';

```

The main route is the one with an actual implementation. Also note that you need to export any controllers/templates in the same fashion if you want to duplicate their behavior.

---

<div class="post-metadata">

### Author: ![kleinfreund](https://avatars.discourse-cdn.com/v4/letter/k/a6a055/32.png) [@kleinfreund](https://meta.discourse.org/u/kleinfreund)
#### Post date: [November 3, 2018, 11:26am UTC](https://meta.discourse.org/t/plugin-with-catch-all-wildcard-route-map/100348/10 "2018-11-03T11:26:24Z")

</div>

Is there any chance Discourse could provide plugins the ability to accomplish this without a workaround?

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [November 3, 2018, 2:08pm UTC](https://meta.discourse.org/t/plugin-with-catch-all-wildcard-route-map/100348/11 "2018-11-03T14:08:35Z")

</div>

Maybe you could use a permalink redirect.
