如何防止 sprocket 缓存 .erb 文件?
Sprockets gem 缓存 .erb 文件,即使这些文件中的 ruby 代码在每次编译时的计算结果可能不同,
例如: foo.js.erb
var foo = <%= Kernel.rand %>;
计算一次并永久缓存。如何防止某些此类文件被 sprocket 缓存?
Sprockets gem caches .erb files even though the ruby code in those might evaluate differently on every compilation
For example: foo.js.erb
var foo = <%= Kernel.rand %>;
evaluates it once and caches forever. How do you prevent certain files like this from being cached by sprockets?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将 JavaScript 分成库(
.js.erb
或只是.js
)和配置数据(例如var foo
)。然后将所有库代码留在 Sprocket 手中,并将您的配置放入正常的 ERB 视图中(可能嵌入到您的布局中)。如果更适合您的架构,您还可以通过单独的控制器(也许是
/config.js
)提供配置数据。这种方法通过将静态库与非静态数据分开来避免整个问题。此外,这种方法非常适合 Rails 3.1 资产管道,您应该在生产部署之前预编译所有内容。
You could separate your JavaScript into libraries (
.js.erb
or just.js
) and configuration data (such as yourvar foo
). Then leave all the library code in the hands of Sprocket and put your configuration into your normal ERB views (probably embedded in your layouts).You could also serve the configuration data through a separate controller (
/config.js
perhaps) if that fits your architecture better.This approach avoids your whole problem by separating static libraries from non-static data. Also, this approach fits nicely with the Rails 3.1 asset pipeline where you're supposed to pre-compile everything before your production deployments.