【问题标题】:Cowboy's static file handler serves old versions of filesCowboy 的静态文件处理程序服务于旧版本的文件
【发布时间】:2015-07-24 00:51:00
【问题描述】:

我正在构建一个包含实时数据的小型 Web 应用程序。数据由 Erlang 使用 Cowboy 提供。为简单起见,我希望 Cowboy 也为网页提供静态文件。但 Cowboy 似乎无法识别磁盘上的文件是否已更改,继续为旧版本提供服务。

我尝试禁用 etags 并清除浏览器的缓存,但没有成功。到目前为止,只有重新启动整个 Erlang 应用程序才能工作,这非常乏味(至少在开发过程中)。有什么方法可以禁用 Cowboy 的缓存机制或让它知道文件更改吗?

牛仔设置代码:

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().

另一方面,具有讽刺意味的是,Cowboy 会根据文件的修改时间和大小仔细生成 etag,但是当请求更改的文件时,它只会回复旧的 etag,而不是检查更改。

最好的问候,克隆乔

【问题讨论】:

    标签: caching erlang cowboy


    【解决方案1】:

    事实证明,relx 在构建版本时复制了 priv_dir,在我继续编辑源文件时提供副本(当 html 文件中的更改无法传递到您的浏览器时,Find 非常有用。)

    我没有在开发过程中构建和运行版本,而是包装了 code:priv_dir/1(类似于 Nikola Skoric shared on the erlang-questions mailing list in 2011)并在开发时跳过版本:

    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().
    

    -module(util).
    
    priv_dir() ->
        case code:priv_dir(app_name) of
            {error, bad_name} ->
                {ok, Cwd} = file:get_cwd(),
                Cwd ++ "/" ++ "priv";
            Priv ->
                Priv
        end.
    

    【讨论】:

    • file:get_cwd() 可能不是最好的选择,如果你想避免这种情况,你可以设置一个环境变量。
    猜你喜欢
    • 2020-08-01
    • 2020-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-22
    • 1970-01-01
    相关资源
    最近更新 更多