2018-04-23 05:27:16 +00:00
# Static Resources (Clojure Only)
2021-03-02 06:39:18 +00:00
Static resources can be served by using the following two functions:
2018-04-23 05:27:16 +00:00
2021-03-02 06:39:18 +00:00
* `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.
2018-04-23 05:27:16 +00:00
## Internal routes
2018-04-23 05:33:44 +00:00
This is good option if static files can be from non-conflicting paths, e.g. `"/assets/*"` .
2018-04-23 05:27:16 +00:00
```clj
(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))
```
2018-04-29 12:29:22 +00:00
To serve static files with conflicting routes, e.g. `"/*"` , one needs to disable the conflict resolution:
2018-04-23 05:33:44 +00:00
```clj
(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))
```
2018-04-23 05:27:16 +00:00
## External routes
2021-03-02 06:39:18 +00:00
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.
2018-04-23 05:27:16 +00:00
```clj
(ring/ring-handler
(ring/router
["/ping" (constantly {:status 200, :body "pong"})])
(ring/routes
(ring/create-resource-handler {:path "/"})
(ring/create-default-handler)))
```
## Configuration
2021-03-02 06:39:18 +00:00
`reitit.ring/create-file-handler` and `reitit.ring/create-resource-handler` take optionally an options map to configure how the files are being served.
2018-04-23 05:27:16 +00:00
2025-03-27 12:17:30 +00:00
| key | description |
| --------------------|-------------|
| :parameter | optional name of the wildcard parameter, defaults to unnamed keyword `:`
| :root | optional resource root, defaults to `\"public\"`
| :path | path to mount the handler to. Required when mounted outside of a router, does not work inside a router.
| :loader | optional class loader to resolve the resources
| :index-files | optional vector of index-files to look in a resource directory, defaults to `[\"index.html\"]`
2025-03-28 14:06:12 +00:00
| :index-redirect? | optional boolean: if true (default false), redirect to index file, if false serve it directly
2025-03-27 12:17:30 +00:00
| :canonicalize-uris? | optional boolean: if true (default), try to serve index files for non directory paths (paths that end with slash)
| :not-found-handler | optional handler function to use if the requested resource is missing (404 Not Found)
2018-10-16 17:32:40 +00:00
2018-04-23 05:27:16 +00:00
### TODO
2018-04-29 13:49:05 +00:00
* support for things like `:cache` , `:etag` , `:last-modified?` , and `:gzip`
2018-04-25 05:32:01 +00:00
* support for ClojureScript