开发者

how to prevent sprockets from caching a .erb file?

开发者 https://www.devze.com 2023-04-09 04:31 出处:网络
Sprockets gem caches .erb files even though the ruby code in those might evaluate differently on every compilation

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 = <%= 开发者_StackOverflowKernel.rand %>;

evaluates it once and caches forever. How do you prevent certain files like this from being cached by sprockets?


You could separate your JavaScript into libraries (.js.erb or just .js) and configuration data (such as your var 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号