reitit/doc/basics/route_conflicts.md

57 lines
1.3 KiB
Markdown
Raw Normal View History

2017-12-10 14:57:09 +00:00
# Route Conflicts
2017-09-18 05:30:03 +00:00
2017-12-27 18:28:03 +00:00
Most routing libraries allow conflicting paths within a router. On lookup, the first match is used making rest of the matching routes effecively unreachable. This is not good, especially if route tree is merged from multiple sources.
2017-09-18 05:30:03 +00:00
2017-12-27 18:28:03 +00:00
Reitit resolves this by running explicit conflicit resolution when a Router is created. Conflicting routes are passed into a `:conflicts` callback. Default implementation throws `ex-info` with a descriptive message.
2017-09-18 05:30:03 +00:00
Examples router with conflicting routes:
2017-09-18 05:30:03 +00:00
```clj
(require '[reitit.core :as r])
2017-09-18 05:30:03 +00:00
(def routes
[["/ping"]
["/:user-id/orders"]
["/bulk/:bulk-id"]
["/public/*path"]
["/:version/status"]])
```
By default, `ExceptionInfo` is thrown:
```clj
(r/router routes)
2017-09-18 05:30:03 +00:00
; CompilerException clojure.lang.ExceptionInfo: Router contains conflicting routes:
;
; /:user-id/orders
; -> /public/*path
; -> /bulk/:bulk-id
;
; /bulk/:bulk-id
; -> /:version/status
;
; /public/*path
; -> /:version/status
;
```
Just logging the conflicts:
```clj
(r/router
2017-09-18 05:30:03 +00:00
routes
{:conflicts (comp println reitit/conflicts-str)})
; Router contains conflicting routes:
;
; /:user-id/orders
; -> /public/*path
; -> /bulk/:bulk-id
;
; /bulk/:bulk-id
; -> /:version/status
;
; /public/*path
; -> /:version/status
;
```