Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement reitit reloading #130

Merged
merged 1 commit into from
Feb 14, 2024
Merged

Conversation

markokocic
Copy link
Member

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
Copy link
Member Author

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
Copy link
Member Author

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

@yogthos yogthos merged commit 364b157 into kit-clj:master Feb 14, 2024
1 check passed
@yogthos
Copy link
Collaborator

yogthos commented Feb 14, 2024

Just pushed up a new template with the changes.

@markokocic markokocic deleted the reitit-reload branch February 14, 2024 11:44
@gerdint
Copy link
Contributor

gerdint commented Feb 18, 2024

@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
Copy link
Member Author

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
Copy link
Contributor

gerdint commented Feb 19, 2024

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
Copy link
Member Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Autoreload on file change or on evalutation
3 participants