Fork me on GitHub
#ring
<
2018-01-16
>
yogidevbear22:01:11

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 , I keep getting the route/not-found route.

seancorfield22:01:44

You put main.css in resources/public/ in your project?

seancorfield22:01:06

(and remove /public from the URL)

seancorfield22:01:33

I believe the default behavior for (route/resources "/") is to assume resources/public/ as the root for assets?

seancorfield22:01:54

> 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?

yogidevbear22:01:08

I was reading through https://learnxinyminutes.com/docs/compojure/ which showed that usage

yogidevbear22:01:02

I didn't have the public folder inside of resources. Moved it and testing again now

yogidevbear23:01:52

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

seancorfield23:01:20

Remove /public from that URL.

yogidevbear23:01:00

Thanks Sean 🙂

seancorfield23:01:06

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

yogidevbear23:01:12

All makes sense now that it's working

seancorfield23:01:33

You can override that behavior by specifying a configuration option map in the route/resources call.

seancorfield23:01:05

Yup, there ya go!

yogidevbear23:01:40

So which library within Clojure knows to look inside resources by default? Is that a Ring thing?

seancorfield23:01:17

and that uses -- which looks on the classpath and the convention is that both resources and src are on your classpath in most Clojure tooling.

yogidevbear23:01:34

TIL 🙂

seancorfield23:01:11

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

seancorfield23:01:31

And we also often have resources/public/favicon.ico for browsers 🙂

seancorfield23:01:55

Other files in resources include EDN config files etc.

yogidevbear23:01:00

And anything inside resources, but outside of public is not accessible via the browser URL field?

yogidevbear23:01:40

e.g. resources/my.edn wouldn't be accessible from or similar route?