serve_file and cache issues

21 views
Skip to first unread message

Golan Hadad

unread,
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 =  "0.0.0.0" ,
         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)
         end,
         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)
          end;
        _ ->
            Req:respond({501, [], []})
end. 

Any suggestions .. Thank you  . 
Golan  



Bob Ippolito

unread,
Dec 14, 2015, 1:48:00 PM12/14/15
to moch...@googlegroups.com
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 mochiweb+u...@googlegroups.com.
To post to this group, send email to moch...@googlegroups.com.
Visit this group at https://groups.google.com/group/mochiweb.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages