reitit

https://cljdoc.org/d/metosin/reitit/ https://github.com/metosin/reitit/
wombawomba 2020-06-06T17:44:21.065700Z

I want to serve static files from a directory on disk in my dev environment (in production I use a CDN). Is there a recommended method of doing this with Reitit? I noticed https://metosin.github.io/reitit/ring/static.html has “serve from file-system” as a TODO item, so I assume I’ll have to find some other approach.

valtteri 2020-06-06T18:46:14.067Z

@wombawomba I’ve done something like this

["/index.html"
      {:get
       {:no-doc true
        :handler
        (fn [_]
          {:status  200
           :headers {"Content-Type" "text/html"}
           :body    (io/input-stream (io/resource "public/index.html"))})}}]
Where io refers to <http://clojure.java.io|clojure.java.io>

👍 1
valtteri 2020-06-06T18:47:07.067800Z

Path could take the filename as a path-parameter or something.

valtteri 2020-06-06T18:47:46.068500Z

Another option is to simply fire up a Jetty server from REPL to serve static files during development. I’ve done just that here https://github.com/ClojureFinland/ClojureFinland.github.io/blob/new-start/dev/server.clj

valtteri 2020-06-06T18:48:35.069200Z

You need to add org.eclipse.jetty/jetty-server {:mvn/version "9.4.29.v20200521"} to your dev-depencencies

ikitommi 2020-06-06T19:54:26.071400Z

there is also reitit.ring/create-file-handler in 0.5.1.

👍 2
wombawomba 2020-06-07T14:39:47.083200Z

I’m struggling with how to use this. It seems like if I don’t specify :root every request results in a NPE.

wombawomba 2020-06-07T14:40:29.083500Z

And when I do specify :root, I can’t figure out how to get it to find my files (everything seems to cause a 404).

wombawomba 2020-06-07T14:40:35.083700Z

Are there any examples for how to use it?

wombawomba 2020-06-07T14:41:02.083900Z

or alternatively, how would I use it to just serve a directory?

wombawomba 2020-06-07T14:55:56.084200Z

Alright, I was able to make it work now by using the “Internal routes” approach from https://metosin.github.io/reitit/ring/static.html instead of the “External routes” one

wombawomba 2020-06-07T14:59:12.084400Z

I think either I’m misunderstanding how the the “External routes” approach is meant to work, or something isn’t working quite right..

wombawomba 2020-06-07T15:15:52.084600Z

Alright, I figured it out. I needed to provide both :root and :path when using the external routes approach. Perhaps the docstring could be rephrased to make it more clear which arguments are required in which situations?

wombawomba 2020-06-07T15:18:15.084800Z

Also, :root defaulting to public seems pretty counterintuitive/surprising to me.. Wouldn’t it make more sense to just force the user to supply :root instead? Feels like it would be better to avoid making these kinds of implicit assumptions about the user’s file structure/intentions.

wombawomba 2020-06-07T17:35:59.085300Z

Oh and another idea, perhaps there should be some sort of validation to check if the root path actually exists?

athomasoriginal 2020-06-06T20:26:23.079600Z

Hey friends, before I log an issue, I wanted to confirm it here first. While using reitit.frontend.easy/start! I noticed that when you trigger an HMR reload with something like figwheel the event listeners are not being removed when the stop method is run. This seems to be because :listen-key and :click-listen-key are not set. Assuming this is not based on how I have this configured my app, I believe I have narrowed the issue down to https://github.com/metosin/reitit/blob/master/modules/reitit-frontend/src/reitit/frontend/easy.cljs#L38. My thinking is that when we reset the history atom to the version of this (the history record) we are currently resetting to does not have the event-listener keys as the on-navigate is called in https://github.com/metosin/reitit/blob/master/modules/reitit-frontend/src/reitit/frontend/history.cljs#L119.

athomasoriginal 2020-06-06T20:27:20.081Z

I cloned the repo locally and implemented a change based on the investigation above and it seems to resolve the issue. Let me know what ya’ll think and I would be happy to log an issue and even make a PR of the fix I implemented.