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.
@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>
Path could take the filename as a path-parameter or something.
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
You need to add org.eclipse.jetty/jetty-server {:mvn/version "9.4.29.v20200521"}
to your dev-depencencies
there is also reitit.ring/create-file-handler
in 0.5.1.
I’m struggling with how to use this. It seems like if I don’t specify :root
every request results in a NPE.
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
).
Are there any examples for how to use it?
or alternatively, how would I use it to just serve a directory?
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
I think either I’m misunderstanding how the the “External routes” approach is meant to work, or something isn’t working quite right..
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?
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.
Oh and another idea, perhaps there should be some sort of validation to check if the root
path actually exists?
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.
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.