reitit

https://cljdoc.org/d/metosin/reitit/ https://github.com/metosin/reitit/
Casey 2020-08-12T11:27:38.460600Z

I'm looking for an example of using reitit on the frontend and backend, where the frontend is an SPA and does not use hash fragments but normal urls. I can't quite figure out how to have the backend serve the frontend on any un-matched route, while still reserving certain routes for the backend (like /api)

Casey 2020-08-13T15:40:33.006500Z

@tkjone thanks! your example works great.

1
Casey 2020-08-13T15:42:36.006800Z

On the frontend side however, how do you initialize when landing on a nested (non-root /) route? I'm using re-frame. The backend is serving the SPA properly now on any path, but on the frontend itself reitit isn't matching the path when I enter the page at a non root path (that is, / works client side but /something/deeper doesn't)

athomasoriginal 2020-08-13T15:44:35.007Z

Do you have a simple example of your routing setup + entry point to your app?

athomasoriginal 2020-08-13T15:45:48.007300Z

Example: this is another simple example of handling front end routing: https://gist.github.com/athomasoriginal/eadc022482c3432943c400cc8eeb1788

Casey 2020-08-13T15:46:01.007500Z

importantly i'm also using :use-fragment false

athomasoriginal 2020-08-13T15:46:46.007700Z

Yeah, the above example should also work with HistoryAPI

Casey 2020-08-13T15:47:07.007900Z

(def router
  (reitit/router
    [""
     ["/" {:name :home :view #'home-ui}]
     ["/something-else" {:name :home :view #'another-ui}]]
    {:compile coercion/compile-request-coercers}))

(defn navigate! [match _]
  (rf/dispatch [:common/navigate match]))


(defn start-router! []
  (rfe/start!
   router
   navigate!
   {:use-fragment false}))

(start-router!)

athomasoriginal 2020-08-13T15:47:43.008100Z

What’s navigate! doing?

Casey 2020-08-13T15:48:41.008600Z

(this is from the luminus project generation with +re-frame)

Casey 2020-08-13T15:49:11.008800Z

(added the navigate! function)

athomasoriginal 2020-08-13T15:51:55.009Z

Yeah, so I just want to see what navigate! is doing. For example, in the gist I provided, mine just updates the match atom. The general idea is i’m trying to isolate where something is going wrong. A quick way to do that is to see if the problem is in your routing logic (reitit) (in my example, routes) or is the problem in the app entry point (in my example current-page)

Casey 2020-08-13T15:52:12.009200Z

so browsing to / loads the :home route, but navigating to /something-else loads a blank page as the match is nil.

Casey 2020-08-13T15:52:40.009400Z

but that only happens on a fresh page load.

Casey 2020-08-13T15:52:54.009600Z

if i load / and then click a button to something-else it works

Casey 2020-08-13T15:54:14.009800Z

Should I be seeding it with the initial window location or does reitit frontend pick that up?

Casey 2020-08-13T15:54:53.010Z

navigate! is called with a nil value for match when browsing to /something-else

athomasoriginal 2020-08-13T15:55:43.010200Z

It should pickup on it. The idea is: • page loads • the url is read + passed to reitit • reitit matches • the match is passed to Reagent I’m not an expert with reitit yet, but can you try updating your routing structure to

[["/" {:name :home :view #'home-ui}]
 ["/something-else" {:name :home :view #'another-ui}]]
Also, why add the #' (var quote) in front of your :views?

pithyless 2020-08-12T13:22:47.466200Z

I'd like to extend reitit.http.coercion/coerce-request-interceptor to also check additional :env data I attach during the processing of a request (think of dynamic per-request things like db and cache connections, etc). Ideally, I'd just re-use the existing reitit-malli plumbing for coercion, but that's only checking for specific keys: https://github.com/metosin/reitit/blob/master/modules/reitit-core/src/reitit/coercion.cljc#L37-L42 This doesn't seem like a far-fetched feature requests - perhaps someone has already done this? Not sure if I should just write a new interceptor, extend the existing malli-coercion interceptor with new ParameterCoercion keys, or perhaps just hack and piggyback on an existing params key.

athomasoriginal 2020-08-12T13:54:42.472700Z

Hey hey! I asked a similar question a little while back and this is what I came up with: https://gist.github.com/athomasoriginal/14c6cfa1500bc1ab1a90a98b9d7217a0 - let me know if this helps!!

valtteri 2020-08-12T13:55:10.473500Z

I have nginx sitting in front that redirects 404s to index.html. I guess you could achieve the same with a default handler that always returns index.html. If you’re using reitit-ring, you can provide the default-handler when creating the router.

athomasoriginal 2020-08-12T13:55:34.473700Z

The general idea is that you have a main set of routes, if anything is not matched by those, you can use the default handler and add in another route check.

athomasoriginal 2020-08-12T13:57:47.473900Z

This is another good place to go to checkout other examples: https://github.com/metosin/reitit/tree/master/examples

Ilya 2020-08-12T15:23:24.474700Z

Hi, is there something like compojure's wrap-routes in reitit? https://weavejester.github.io/compojure/compojure.core.html#var-wrap-routes

ikitommi 2020-08-12T15:30:05.479300Z

@ilyab you can do ~same by mounting middleware to specific routes or methods, so no.

pithyless 2020-08-12T15:30:58.480700Z

@ikitommi thanks for the pointer!

ikitommi 2020-08-12T15:31:51.481700Z

wrap-routes caused lot of troubles with nginx-clojure back in the days.

Ilya 2020-08-12T15:35:59.482200Z

Thanks @ikitommi!

haywood 2020-08-12T19:46:50.483500Z

anyone using reitit frontend easy with Html5History and have back / forward buttons ‘just working’? Should I be calling pushState myself in my on-navigate function?

haywood 2020-08-12T19:58:25.484200Z

how do I get <a href="" tags to not cause a page refresh?