kit-clj / kit

Lightweight, modular framework for scalable web development in Clojure
https://kit-clj.github.io/
MIT License
463 stars 43 forks source link

Implement reitit reloading #130

Closed markokocic closed 4 months ago

markokocic commented 4 months ago

Fixes #48 by implementing reloading of routes as recommended by reitit in https://github.com/metosin/reitit/blob/master/doc/advanced/dev_workflow.md

To take advantage of reloading, one has to return a function instead of a vector in his route definition. Check (defmethod ig/init-key :reitit.routes/api as an example.

The solution is backward compatible: it still accepts vectors as a route definition, but in that case no reloading.

The reloading is by default enabled only in :dev profile.

@yogthos , @nikolap please review before merging. I did some testing, but I'm not sure if there is a better/cleaner way to hook it up in integrant.

markokocic commented 4 months ago

Once merged, one would also need to update all route definitions, e.g. in modules and docs to take advantage of reloading.

I could not find a way to enable reloading of routes while still keeping route definitions returning vectors,

markokocic commented 4 months ago

Moved dev mode check in init-key for :router/core and changed (vec (map ... to (mapv ...

yogthos commented 4 months ago

Just pushed up a new template with the changes.

gerdint commented 4 months ago

@markokocic Hi I wanted to try this. I didn't read through the Reitit docs on it but couldn't get any re-evaluations I did take effect. Do you need to re-eval the actual handler or does any function down the call chain suffice? (I would guess so) I created a new project with clj-new.

markokocic commented 4 months ago

HI @gerdint , you need to re-evaluate only what you changed in repl, cider, nrepl for your changes to be picked up. Just saving the file does nothing.

You also need ot make sure init-key function for your route returns a fn instead of vector, like for example in api:

(defmethod ig/init-key :reitit.routes/api
  [_ {:keys [base-path]
      :or   {base-path ""}
      :as   opts}]
  (fn [] [base-path route-data (api-routes opts)]))
gerdint commented 4 months ago

Hmm, this is exactly what I (thought) I did. Will have another go. It would save a lot of cpu cycles not to have to (reset) all the time.

markokocic commented 4 months ago

I forgot to add that you have to be in the devmode for the hot-reloading of routes, for example clj -M:dev:cider

In prod mode, routes are cached.