Adding cache-control and other headers (per route)

166 views
Skip to first unread message

Casper Clausen

unread,
Aug 26, 2015, 6:45:25 AM8/26/15
to Liberator
Hi

I would like to be able to add cache-control headers (and possibly other headers) that are specific for the given route (meaning that it can't be done in a wrapper).

What would be the idiomatic way of doing this in Liberator?

/Casper

Philipp Meier

unread,
Aug 26, 2015, 9:22:06 AM8/26/15
to Liberator
Hi,


Am Mittwoch, 26. August 2015 12:45:25 UTC+2 schrieb Casper Clausen:
Hi

I would like to be able to add cache-control headers (and possibly other headers) that are specific for the given route (meaning that it can't be done in a wrapper).

First of all you can wrap individual handlers in a route definition (e.g. for compojure):

 
(defn wrap-respoonse-header [h header value] (fn [req]
(assoc-in (h req) [:headers header] value)))
(defresource foo-resource
:handle-ok "FOO!")
(defroutes app
(ANY "/FOO" [] (wrap-response-header foo-resource "X-TEST" "whatever")))

But liberator also offers a way to "update" the ring response created from the representation returned from a handler:

(defresource foo-resource
:handle-ok (fn [_] (ring-response "FOO" {:headers "X-Test" "whatever"})))

More details in the documentation.

-billy.

Casper Clausen

unread,
Aug 26, 2015, 10:24:17 AM8/26/15
to Liberator
Thanks for the swift response.

I have been experimenting with the :as-response thing which is working except for Cache-Control, which is overwritten as:

Cache-Control: no-cache, no-store, no-cache, must-revalidate, post-check=0, pre-check=0

I just tried your suggestion with wrap-response-header, and it the same thing. Custom headers work, but Cache-Control is overwritten somewhere so it always gives the above.

I also disabled all middlewares to exclude anything that we might be doing in there.

/Casper

Casper Clausen

unread,
Aug 26, 2015, 10:34:45 AM8/26/15
to Liberator
Here is a simple example of what I am seeing:

(defroutes app-test

  (wrap-respoonse-header (ANY "/foo" [] (resource :available-media-types ["text/html"]

                            :handle-ok "<html>Hello, Internet.</html>"))

                         "Cache-Control" "max-age=60s"))


and then using ring.server.standalone to service the route.


Pragma: no-cache
Date: Wed, 26 Aug 2015 14:31:39 GMT
Last-Modified: Wed, 26 Aug 2015 14:31:38 GMT
Server: nginx/1.8.0
Vary: Accept
Content-Type: text/html;charset=UTF-8

Cache-Control: no-cache, no-store, no-cache, must-revalidate, post-check=0, pre-check=0

Connection: keep-alive
Content-Length: 29
Expires: Wed, 26 Aug 2015 14:31:38 GMT


<html>Hello, Internet.</html>

Philipp Meier

unread,
Aug 26, 2015, 4:05:07 PM8/26/15
to Liberator
Hi,


Am Mittwoch, 26. August 2015 16:34:45 UTC+2 schrieb Casper Clausen:
Here is a simple example of what I am seeing:

(defroutes app-test

  (wrap-respoonse-header (ANY "/foo" [] (resource :available-media-types ["text/html"]

                            :handle-ok "<html>Hello, Internet.</html>"))

                         "Cache-Control" "max-age=60s"))


and then using ring.server.standalone to service the route.


Pragma: no-cache
Date: Wed, 26 Aug 2015 14:31:39 GMT
Last-Modified: Wed, 26 Aug 2015 14:31:38 GMT
Server: nginx/1.8.0
Vary: Accept
Content-Type: text/html;charset=UTF-8
Cache-Control: no-cache, no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection: keep-alive
Content-Length: 29
Expires: Wed, 26 Aug 2015 14:31:38 GMT


<html>Hello, Internet.</html>

That's weird. My first suspect is nginx here, maybe it adds the response headers? To debug this I would recommend to run your resource in the repl:

> ((app-test) { :request-method :get :uri "/foo" })

What does this return?

Casper Clausen

unread,
Aug 26, 2015, 5:15:51 PM8/26/15
to Liberator
Ah yes! I totally forgot about the nginx. That is probably it actually, I'll try it without it in the morning.

/Casper

Casper Clausen

unread,
Aug 27, 2015, 3:37:51 AM8/27/15
to Liberator
Yup, I am a moron. It works fine when not going though the nginx server. Thanks for the help!
Reply all
Reply to author
Forward
0 new messages