Cowboy's static file handler serves old versions of files

时间:2015-05-12 23:01:22

标签: caching erlang cowboy

I am building a small web application which is fed with realtime data. The data is provided from Erlang using Cowboy. For simplicity, I'd like Cowboy to also serve the static files for the web page. But it seems Cowboy does not recognize if files are changed on disk, continuing to serve the old version.

I have tried disabling etags and clearing the browser's cache, to no success. So far only restarting the whole Erlang application works, which is very tedious (at least during development). Is there some way to disable Cowboy's caching mechanism or make it aware of the file changes?

Cowboy setup code:

xsl:for-each

On a side note, it's ironic that Cowboy carefully generates etags based on a file's modification time and size but when a changed file is requested it just replies with the old etag instead of checking for changes.

Best regards, clonejo

1 个答案:

答案 0 :(得分:1)

It turned out that relx copied the priv_dir when building a release, serving the copies while I continued editing the source files (Find is very helpful when changes in html files do not get through to your browser.)

Instead of building and running releases during development I have wrapped start(_StartType, _StartArgs) -> % setup cowboy Dispatch = cowboy_router:compile([{'_', [{"/", cowboy_static, {priv_file, app_name, "static/index.html"}}, {"/[...]", cowboy_static, {priv_dir, app_name, "static"}}]}]), {ok, _} = cowboy:start_http(http_listener, 5, [{port, 12345}], [{env, [{dispatch, Dispatch}]}]), sup:start_link(). (similar to what Nikola Skoric shared on the erlang-questions mailing list in 2011) and skip releases while developing:

code:priv_dir/1

start(_StartType, _StartArgs) ->
    % setup cowboy
    Dispatch = cowboy_router:compile([{'_',
                                       [{"/", cowboy_static, {file, util:priv_dir() ++ "/static/index.html"}},
                                        {"/[...]", cowboy_static, {dir, util:priv_dir() ++ "/static"}}]}]),
    {ok, _} = cowboy:start_http(http_listener, 5, [{port, 12345}], [{env, [{dispatch, Dispatch}]}]),
    sup:start_link().