deps.edn
res
in this context is short for resources
. I like using res
because it has the same number of letters as src
and dev
. That's just personal
preference though.
The reason we add a new path instead of putting static resources in src
- which would work -
is partially preference, but also partially because at a certain point you might want to ahead of time compile
your Clojure code. In that context you would also want to copy over your static resources. It's just a smidge
easier to copy a folder than it is to copy all files which don't have a specific file extension.
{:paths ["src" "res"]
:deps {org.clojure/clojure {:mvn/version "1.12.0"}
...}}
res/favicon.ico
.Save favicon.ico and put it in the res
folder. If you didn't
make a res
folder on the previous step, do so now.
src/example/static/routes.clj
for static routes
This code, as written, only has a route handler for /favicon.ico
.
It is quite a bit more popular to instead have one handler with a wildcard parameter. Something like public/*
that will look up any file that is under some public
folder.
This is valid, and one function which does that is reitit.ring/create-resource-handler
. If you end up finding this approach onerous,
use that.
The pros of serving things with explicit handlers are:
favicon.ico
, but you can maybe imagine a page that has a logo specific for that page./*
work out.The cons are, I think, a bit more obvious.
(ns example.static.routes
(:require [ring.util.response :as response]))
(defn favicon-ico-handler
[& _]
(response/resource-response "/favicon.ico"))
(defn routes
[_]
[["/favicon.ico" favicon-ico-handler]])
(ns example.routes
(:require [clojure.tools.logging :as log]
[example.cave.routes :as cave-routes]
[example.goodbye.routes :as goodbye-routes]
[example.hello.routes :as hello-routes]
[example.static.routes :as static-routes]
[example.system :as-alias system]
[hiccup2.core :as hiccup]
[reitit.ring :as reitit-ring]))
(defn routes
[system]
[""
(static-routes/routes system)
(cave-routes/routes system)
(hello-routes/routes system)
(goodbye-routes/routes system)])
...
http://localhost:9999
You should see the tiny favicon on the browser tab. This means that we successfully served up the file.
Make sure to "hard refresh" if it doesn't work right away. The browser likes to cache things.