Static Resources

xificurC edited this page Aug 2, 2015 · 6 revisions

Web applications often need to serve static content, such as images or stylesheets. Ring provides two middleware functions to do this.

One is wrap-file. This serves static content from a directory on the local filesystem:

(use 'ring.middleware.file)
(def app
  (wrap-file your-handler "/var/www/public"))

The other is wrap-resource. This serves static content from the JVM classpath:

(use 'ring.middleware.resource)
(def app
  (wrap-resource your-handler "public"))

If you're using a Clojure build tool like Leiningen then the non-source-file resources for a project are kept in the resources directory. Files in this directory are automatically included in jar or war files.

So in the above example, files placed in the resources/public directory will be served up as static files.

Often you'll want to combine wrap-file or wrap-resource with other middleware, usually wrap-content-type and wrap-not-modified:

(use 'ring.middleware.resource

(def app
  (-> your-handler
      (wrap-resource "public")

The wrap-content-type middleware chooses a content-type based on the file extension. For instance, a file called hello.txt would get a content-type of text/plain.

The wrap-not-modified middleware checks the Last-Modified header on the response against the If-Modified-Since header on the request. This saves bandwidth by ensuring clients don't need to download resources they've already cached.

Note that this extra middleware needs to wrap around (i.e. come after) the wrap-resource or wrap-file functions.

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.