reitit

2024-11-04T16:17:55.252709Z

Does anyone have any examples of nested routes with more than 2 levels of nesting?

2024-11-04T16:18:26.034199Z

I'm surely doing something syntactically wrong but I can't quite figure it out ...

2024-11-04T16:25:32.295959Z

For example, why doesn't the other nesting work here?

2024-11-04T16:25:36.972489Z

(def some-routes
  ["/" {}
   ["" {:name :legacy}]
   ["-/team" {:name :main}
    ["/:team-id"] {:name :team}]])

(r/route-names (r/router some-routes)) ;; => [:legacy :main]

2024-11-04T16:25:57.448269Z

I don't understand why :team is not there.

hadils 2024-11-04T16:26:47.317479Z

I think there’s a dash before /team

2024-11-04T16:27:30.010919Z

There is intended to be -

2024-11-04T16:27:42.415609Z

I want the URL to be like "/-/team/12312"

hadils 2024-11-04T16:28:03.046189Z

Oh. Sorry.

2024-11-04T16:29:07.300459Z

Oh, no apology needed, certainly - I'm sure I'm making a mistake here, but I've combed the reitit docs and examples and even at the REPL I'm not managing to see why (in terms of syntax) the second level of nesting doesn't result in :team being part of what route-names returns.

hadils 2024-11-04T16:52:18.680919Z

Perhaps the {:name :team} should be inside the ["/:team-id"] array?

wevrem 2024-11-04T17:40:24.049809Z

It’s because of the way reitit flattens routes. You need a "" route.

["-/team"     {:options-shared-by :all-children}
 [""          {:name :main}]
 ["/:team-id" {:name :team}]]

2024-11-04T18:00:38.505869Z

Thank you @michaeljweaver - but how to have a different route for a plain / ?

2024-11-04T18:00:50.413779Z

(That's why I tried to show 3 levels in my example)

2024-11-04T18:01:20.668499Z

Ah --

2024-11-04T18:01:30.855189Z

(def routes
  ["/" {}
   ["" {:name :legacy}]
   ["-/team"     {:options-shared-by :all-children}
    [""          {:name :main}]
    ["/:team-id" {:name :team}]]])

2024-11-04T18:01:38.549529Z

That appears to work, at least from the standpoint of the route names.

wevrem 2024-11-04T18:01:47.060379Z

I didn’t reproduce your full tree, just that one branch.

2024-11-04T18:01:55.782079Z

Ok - thank you for your help!

2024-11-04T18:02:17.842359Z

I wish I understood this better - maybe reading the reitit code is the only path.

wevrem 2024-11-04T18:02:25.729319Z

It’s tripped me up before too, because I think of a tree with parents and children, but reitit flattens everything and only preserves the children.

wevrem 2024-11-04T18:02:56.624359Z

So if you want a route that represents the parent, you have to use "".

wevrem 2024-11-04T18:04:10.528949Z

There is some other function, besides route-names, that will let you examine the final generated routes?

2024-11-04T18:04:34.077379Z

I'm only trying to do this nested stuff because I wanted this sort of hierarchal controller start/stop behavior -

2024-11-04T18:04:43.587589Z

But can I get the equivalent behavior by just writing this flat?

2024-11-04T18:05:04.635819Z

Because I'm trying to add one level further (like, with "/:foo-id") and I'm still in the same boat as before,

2024-11-04T18:05:10.001909Z

but somewhat unable to generalize your advice.

wevrem 2024-11-04T18:05:12.291859Z

I think you can, but I think nesting is better. You will have options you want for some children, but not others, and nesting is really handy for that.

2024-11-04T18:06:00.400529Z

Ah I have an idea.

wevrem 2024-11-04T18:07:01.577469Z

I’ve used this to examine my final routes:

(comment
  (do
    (require '[reitit.core :as r])
    (r/routes (reitit/router routes {:conflicts nil})))
  )

2024-11-04T18:09:56.952579Z

Thank you @michaeljweaver!