防止缓存单个js文件 [英] Prevent caching of a single js file

查看:214
本文介绍了防止缓存单个js文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的资产目录中有一个.js.erb文件.这将读取一些特定于语言环境的配置.

I have a .js.erb file in my assets directory. This reads some locale specific config.

但是,当基础配置更改时,新文件将不提供给我的浏览器.我得到304未修改.

However when the underlying config changes the new file is not being served to my browser. I get 304 not modified.

如果我通过添加空格来更改.js.erb文件,则正确提供了新文件.每次添加配置时都要这样做.

If I change the .js.erb file by adding white space the new file is correctly served. Doing that each time I add config would be a pain.

有没有一种方法可以配置rails只是不缓存该特定文件?

Is there a way to configure rails just to not cache this particular file?

谢谢您的建议.

对资产管道进行了更多的阅读

资产在服务器启动后在第一个请求上进行编译和缓存.Sprockets设置了一个必须重新验证的Cache-Control HTTP标头,以减少后续请求的请求开销-在这些请求上,浏览器得到304(未修改)响应

"Assets are compiled and cached on the first request after the server is started. Sprockets sets a must-revalidate Cache-Control HTTP header to reduce request overhead on subsequent requests — on these the browser gets a 304 (Not Modified) response.

如果清单中的任何文件在两次请求之间均已更改,则服务器将以新的编译文件进行响应."

If any of the files in the manifest have changed between requests, the server responds with a new compiled file."

所以这里的问题是第一个请求被缓存了-这是一个动态的javascript文件.也许是防止这种情况缓存到内联javascript的唯一方法?

So the problem here is the first request is cached - this is a dynamic javascript file. Is perhaps the only way to prevent this being cached to inline the javascript??

推荐答案

听起来您不想使用资产管道,该管道正在非常努力地促进缓存.

It sounds like you don't want to use the asset pipeline, which is trying very hard to promote caching.

创建实际的控制器,而不是试图破坏资产管道,然后您可以根据自己的喜好设置缓存控制标头.

Rather than trying to fight the asset pipeline, create an actual controller and then you'll be able to set the cache control headers to your liking.

如果app/views/controller_name中有一个js.erb模板,那么rails应该只渲染它.

If you have a js.erb template in app/views/controller_name then rails should just render it.

例如,如果您有一个名为JsController的控制器,则可以添加

For example if you had a controller called JsController you could add

match '/javascripts/settings.js', :controller => :js, :action => :settings, :format => :js, :as => :setting_js

到您的routes.rb文件,然后将settings.js.erb粘贴在app/views/js中

to your routes.rb file and then stick settings.js.erb in app/views/js

然后您可以使用

= javascript_include_tag settings_js_path

这篇关于防止缓存单个js文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆