serve_file and cache issues

Skip to first unread message

Golan Hadad

Dec 14, 2015, 12:13:57 PM12/14/15
to MochiWeb
Hi , I need some help ... 
I had install the mochiweb app alongsideand other apps and master app server ( diameter server ) and use it to serve static files 
the Req:serve_file sends the content to clients but when i change the content of the files 
mochiweb still responses with the  old file content ... 
I've tried restart with no help .
Req:cleanup() not help as well .. 

The only thing  that help is to shutdown the server and re-compiled it and start the server again ... 

my code : 
init() ->
         Ip =  "" ,
         Port  = 8000 ,
         Options = [ {ip, Ip } ,{port, Port}, {docroot, local_path(["priv", "www"])}
         {DocRoot, Options1} = get_option(docroot, Options),
         Loop = fun (Req) ->
                   ?MODULE:loop(Req, DocRoot)
         mochiweb_http:start([{name, ?MODULE}, {loop, Loop} | Options1]) .

loop(Req, DocRoot) ->
    "/" ++ Path = Req:get(path),
        case Req:get(method) of
          Method when Method =:= 'GET'; Method =:= 'HEAD' ->
          case Path of
                "cleanup" ->   Req:cleanup(), Req:ok({"application/json", [] , [ jsonx:encode([{status,true}] ) ]   });
                _ ->   Req:serve_file(Path, DocRoot)
        _ ->
            Req:respond({501, [], []})

Any suggestions .. Thank you  . 

Bob Ippolito

Dec 14, 2015, 1:48:00 PM12/14/15
There's a three argument version of Req:serve_file that takes a list of extra headers to send, so you can send whatever you need to ensure that the files do not get cached by the browser.

You can try something like this if you do not want the browser to cache anything:

Req:serve_file(Path, DocRoot, [
  {"Cache-Control", "no-cache, no-store, must-revalidate"},
  {"Pragma", "no-cache"},
  {"Expires", "0"}])

By default it sends last-modified headers, but your browser must not be respecting that.

Req:cleanup() has absolutely no relevance whatsoever to this, you should never have that in your code. It is an implementation detail that happens behind the scenes that is only relevant to Keep-Alive requests.

You received this message because you are subscribed to the Google Groups "MochiWeb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To post to this group, send email to
Visit this group at
For more options, visit

Reply all
Reply to author
0 new messages