Fork me on GitHub
#reitit
<
2020-06-06
>
wombawomba17:06:21

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.

valtteri18:06:14

@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

👍 4
valtteri18:06:07

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

valtteri18:06:46

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

valtteri18:06:35

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

ikitommi19:06:26

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

👍 8
wombawomba14:06:47

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

wombawomba14:06:29

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).

wombawomba14:06:35

Are there any examples for how to use it?

wombawomba14:06:02

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

wombawomba14:06:56

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

wombawomba14:06:12

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

wombawomba15:06:52

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?

wombawomba15:06:15

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.

wombawomba17:06:59

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

athomasoriginal20:06:23

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.

athomasoriginal20:06:20

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.