Liking cljdoc? Tell your friends :D

Static Resources (Clojure Only)

Static resources can be served by using the following two functions:

  • reitit.ring/create-resource-handler, which returns a Ring handler that serves files from classpath, and
  • reitit.ring/create-file-handler, which returns a Ring handler that servers files from file system

There are two ways to mount the handlers. The examples below use reitit.ring/create-resource-handler, but reitit.ring/create-file-handler works the same way.

Internal routes

This is good option if static files can be from non-conflicting paths, e.g. "/assets/*".

(require '[reitit.ring :as ring])

(ring/ring-handler
  (ring/router
    [["/ping" (constantly {:status 200, :body "pong"})]
     ["/assets/*" (ring/create-resource-handler)]])
  (ring/create-default-handler))

To serve static files with conflicting routes, e.g. "/*", one needs to disable the conflict resolution:

(require '[reitit.ring :as ring])

(ring/ring-handler
  (ring/router
    [["/ping" (constantly {:status 200, :body "pong"})]
     ["/*" (ring/create-resource-handler)]]
    {:conflicts (constantly nil)})
  (ring/create-default-handler))

External routes

A better way to serve files from conflicting paths, e.g. "/*", is to serve them from the default-handler. One can compose multiple default locations using reitit.ring/ring-handler. This way, they are only served if none of the actual routes have matched.

(ring/ring-handler
  (ring/router
    ["/ping" (constantly {:status 200, :body "pong"})])
  (ring/routes
    (ring/create-resource-handler {:path "/"})
    (ring/create-default-handler)))

Configuration

reitit.ring/create-file-handler and reitit.ring/create-resource-handler take optionally an options map to configure how the files are being served.

keydescription
:parameteroptional name of the wildcard parameter, defaults to unnamed keyword :
:rootoptional resource root, defaults to \"public\"
:pathpath to mount the handler to. Required when mounted outside of a router, does not work inside a router.
:loaderoptional class loader to resolve the resources
:index-filesoptional vector of index-files to look in a resource directory, defaults to [\"index.html\"]
:not-found-handleroptional handler function to use if the requested resource is missing (404 Not Found)

TODO

  • support for things like :cache, :etag, :last-modified?, and :gzip
  • support for ClojureScript

Can you improve this documentation? These fine people already did:
Tommi Reiman, Miikka Koskinen & Tharaka Manawardhana
Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close