在Rails 3.1中以开发模式启用图像缓存 [英] Enable image caching in development mode in Rails 3.1

查看:102
本文介绍了在Rails 3.1中以开发模式启用图像缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Rails 3.1开发模式下(使用资产管道时),从资产/图像提供的图像通过响应标头缓存控制:必须重新验证来提供。



这意味着Google Chrome(以及似乎只有Chrome)会尝试多次重新获取图像 - 即使在单页面视图中也是如此。这导致了所有通过JavaScript操作DOM操作的棘手问题。仅举几例:


  • jQuery UI Draggable有时会与鼠标光标产生显着的偏移量
  • 添加或删除引用图像的CSS类将会闪烁或调整大小,而图像请求(将始终返回304未修改)正在进行中。

  • 附加或替换包含图像的HTML节点将触发更多的图像抓取,这将导致Chrome浏览器等待每个图像的304响应时,它们下方的整个节点树会出现口吃。



<我可以完全理解这对于开发服务器来说是合理的。我甚至可以理解,即使在单个页面视图内,Chrome拒绝缓存图像也是合理的。



那么,有没有办法改变Rails应用于开发中的图像响应的缓存控制头?

更新:如同一对夫妇所建议的那样,更有趣的问题是为什么Chrome浏览器尝试在浏览量中多次重新提取图片,而其他浏览器似乎没有? (为什么这不会导致其他开发者的问题?)

更新x2:我不打算将此作为答案提交因为这只是一种解决方法,足以达到我的目的,但是我们能够通过预编译资源并放弃预编译的CSS& JS。 (这将需要在 development.rb 中将链轮调试转换为false。)

  rake资产:预编译
cd public / assets
find。 -name* .js *-exec rm -rf {} \;
找到。 -name* .css *-exec rm -rf {} \;


解决方案

http://code.google.com/p/chromium/issues/detail?id=102706



这似乎是使用chrome记录的问题。我遇到了同样的问题:添加或删除引用图像的CSS类将会闪烁或调整大小,而图像请求(它总是会返回304没有修改)正在进行中。


In Rails 3.1 development mode (when using the asset pipeline), images served out of assets/images are served up with the response header "Cache Control: must-revalidate".

This means that Google Chrome (and seemingly only Chrome) will attempt to re-fetch images numerous times—even during a single page view. This has resulted in screwy issues with all manners of DOM manipulation via JavaScript. To name a few:

  • jQuery UI Draggable sometimes features a dramatic offset from the mouse cursor
  • Adding or removing a CSS class that references an image will flash or resize while the image request (which will always return a 304 not modified) is underway.
  • Attaching or replacing HTML nodes that contain images will trigger more image fetches which will cause the entire tree of nodes underneath them to flash stutter as Chrome waits for the 304 response for each image.

I can completely understand that to be a reasonable thing for a development server to do. I can even understand that Chrome's refusal to cache the image, even inside a single page view, is perfectly reasonable.

So, is there a way to change the Cache Control header that Rails applies to image responses in development?

Update: as suggested by a couple people, an even more interesting question is why does Chrome attempt to re-fetch the images multiple times within a pageview when no other browsers seem to? (And why isn't this causing issues for other developers?)

Update x2: I'm not going to submit this as an answer because it's just a workaround that happens to be sufficient for my purposes, but we were able to get around this issue by precompiling assets and then discarding the precomplied CSS & JS. (This will require sprockets debugging to be turned to false in development.rb.)

rake assets:precompile
cd public/assets
find . -name "*.js*" -exec rm -rf {} \;
find . -name "*.css*" -exec rm -rf {} \;

解决方案

http://code.google.com/p/chromium/issues/detail?id=102706

This seems to be a documented issue with chrome. I'm suffering from the same problem: Adding or removing a CSS class that references an image will flash or resize while the image request (which will always return a 304 not modified) is underway.

这篇关于在Rails 3.1中以开发模式启用图像缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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