Hello everyone. I'm trying to figure out the serving of static files (e.g. css / js) within compojure. My current understanding is that I should be making use of the compojure.route/resources
function. I've defined (route/resources "/")
inside of my (defroutes app ...
declaration. I also added (app {:uri "/public/main.css" :request-method :get})
after the defroutes
function. If I try browsing to <http://localhost:12345/public/main.css>
, I keep getting the route/not-found
route.
You put main.css
in resources/public/
in your project?
(and remove /public
from the URL)
I believe the default behavior for (route/resources "/")
is to assume resources/public/
as the root for assets?
> I also added (app {:uri "/public/main.css" :request-method :get})
after the defroutes
function.
Why did you do that @yogidevbear? Just to test in the REPL what it will do?
I was reading through https://learnxinyminutes.com/docs/compojure/ which showed that usage
I didn't have the public
folder inside of resources
. Moved it and testing again now
So I have resources/public/main.css
in my example. I have (route/resources "/")
in my defroutes
, and I've removed (app {:uri "/public/main.css" :request-method :get})
. I'm still seeing page not found when trying to access <http://localhost:12345/public/main.css>
Remove /public
from that URL.
ah
Bingo
Thanks Sean 🙂
The public
part is on the file system (so web-accessible stuff is in that folder and non-web-accessible resources such as config files are outside it).
All makes sense now that it's working
You can override that behavior by specifying a configuration option map in the route/resources
call.
In particular this part https://github.com/weavejester/compojure/blob/master/src/compojure/route.clj#L22-L23
Yup, there ya go!
Actually that's for files
, https://github.com/weavejester/compojure/blob/master/src/compojure/route.clj#L41 is for resources
So which library within Clojure knows to look inside resources
by default? Is that a Ring thing?
The compojure middleware defers to https://github.com/ring-clojure/ring/blob/master/ring-core/src/ring/util/response.clj#L320
and that uses <http://clojure.java.io/resource|clojure.java.io/resource>
-- which looks on the classpath and the convention is that both resources
and src
are on your classpath in most Clojure tooling.
TIL 🙂
In a typical web app at work, we have resources/public/assets
containing css
, img
, and js
subfolders -- and URLs have /assets/css
etc -- and we have resources/templates
containing HTML files that are used as templates and rendered via Selmer (our preferred templating engine -- inspired by Django).
And we also often have resources/public/favicon.ico
for browsers 🙂
Other files in resources
include EDN config files etc.
And anything inside resources
, but outside of public
is not accessible via the browser URL field?
e.g. resources/my.edn
wouldn't be accessible from <http://example.com/my.edn|example.com/my.edn>
or similar route?