Fork me on GitHub
#reitit
<
2022-03-12
>
craftybones07:03:33

Quick question. What is the canonical way to access route data within a handler?

Ben Sless10:03:38

by default inject-match? is true so the data will be in the injected Match which the handler can access If you compile, you can access that data at compile time, too, so you can close over relevant data before returning an interceptor

craftybones10:03:36

So the right key to look at in the context is :reitit.core/match

craftybones10:03:48

Is that correct?

Ben Sless10:03:52

Should be, yes

craftybones10:03:28

And on compiled routes, there’s no real need to specify a handler right? The compilation yields a handler effectively?

Ben Sless10:03:29

No, the handler is part of the data and is required, afaict

craftybones10:03:29

Interesting. Why does the compiler fire twice in this example?

craftybones10:03:57

(defn handler [req]
  {:status 200
   :body   "pong"})

(def routes ["/ping" {:name ::ping
                      :get  {:handler handler}
                      :interceptors [{:name ::interceptor
                                      :compile (fn [ctx _]
                                                 (println "compiler " ctx)
                                                 {:enter identity
                                                  :leave identity})}]}])

(http/router routes)

craftybones10:03:22

I mean, I made this up, but why does the compile trigger twice?

craftybones10:03:54

The second time it is called it seems to also add the :no-doc? key in the ctx

Ben Sless10:03:19

This gets a bit too far into the implementation details than I know

craftybones07:03:14

ring/get-match ?

craftybones07:03:19

Also, what does compilation mean for an interceptor based approach?

craftybones12:03:22

I’ve figured most of it out, thanks to @ben.sless

craftybones12:03:48

One question remains. Why is the compile function invoked multiple times when http/routes is called?

craftybones07:03:51

I’ve looked at the code and I am still not sure why the compile function is invoked twice. I’ve no problems with it, but just trying to understand the process. http/router invokes a middleware/compile-routes

Oliver Marks19:03:11

whats the recommended way to changing a route in reitit-frontend pragmatically I am using push-state but I am noticing that does not trigger the routers navigate function so does not reload the page correctly where as a clicked rfe/href does.

Oliver Marks14:03:43

Found out this was due to hot reloading not working correctly, so not related to reitit push-state calls -on-navigate manually when called.