Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Chrome is not caching images

I'm trying to optimize my application in Ruby on Rails, and I realized that the pictures in my application is what most long does it take to load, but I also noticed another problem, which is that google chrome isn't caching the images.

I noted this because in the Google Developers Console you can see that Google Chrome makes a request to load the images that are canceled before the images are truly loaded.

This can be seen here, first I open the Google Developers Console, then refresh the page and within the first requests there you can see the ones of the images, but they are canceled immediately.

google_not_caching

After that you can see the requests that actually loaded the images.

enter image description here

I don't understand why is this happening if in the response headers you can see that the Cache Control is set to public with max-age = 31536...

response_header

I put the images in my application this way:

<div class="col-xs-3"><%= image_tag "#{@hero.id}/ability_1.png", class: "center-block"%></div>

And the images are organized in folders in app/assets/images

Is there a RoR way to fix this?

Edit: Now testing my app (which is in Heroku) in Windows I noticed that in fact Google Chrome caches the images sometimes, but it happens like the 50% of the times (and when I was in Ubuntu in development it didn't work a single time), while in firefox the first time the images are loaded, but the subsequent times I load the same view I can't even notice the reload, it's beatiful, Why google Chrome is not like that? Is normal that Google Chrome acts so weird?

like image 297
Patricio Sard Avatar asked Feb 13 '16 02:02

Patricio Sard


People also ask

How to fix images not showing in Google Chrome?

Images not showing in Chrome might be related to Chrome’s data folder as well. Renaming the folder is a way to fix it. Step 1: You need to close Google Chrome first. Step 2: Press Windows + R to open Run window. Step 3: Type in %localappdata% and click OK.

Does chrome cache image files?

caching - Chrome doesn't cache images/js/css - Stack Overflow When Chrome loads my website, it checks the server for updated versions of files before it shows them.

How do I know if chrome is caching JS/CSS?

Note chrome caches JS/CSS by default, without a cache-control header, at least for regular page loads. On reload or shift-reload it fetches new from the server. – Bryce Jan 9 '14 at 17:09 On devconsole, if chrome loads cached file you should see (from cache) on the size column against the file

How do I enable or disable cache in Google Chrome?

Open Google Chrome and navigate to the page you want to test. press F12 or open developer tools from within Chrome's settings (Settings > More tools > Developer tools). Click the cog in the top right of the pop-out box. Check the "Disable Cache...


2 Answers

The most important thing to realize when analyzing browser caching is the "Status Code". In your example, you can see you got a "304", which stands for "Not Modified" Which means the browser "could potentially use it's cache". So you ARE in fact caching. Caching != Not hitting your web server.

The definition according to Mozilla:

This is used for caching purposes. It is telling to client that response has not been modified. So, client can continue to use same cached version of response.

It sends the etag and last-modified to your web server, and your web server then looks at those meta and say "Nope, this file hasn't changed, so feel free to use your cache", and that's it. It actually does not send the file again. You can see that the "Size" is much less then when it's a "200" status code, where the web server IS sending the file, and the timing should me much shorter as well.

In Chrome you can force "non-caching" by checking the "Disable cache" option in the Network tab.

Hope that helps!

like image 126
omarvelous Avatar answered Oct 05 '22 22:10

omarvelous


It looks like Chrome does handle image caching differently. What type of reload are you doing (following links, pressing enter in the address bar, Ctrl+r)? It looks like if you press enter in the search bar it will respect max-age but if you use Ctrl+r Chrome sets max-age to 0.

expires_in max-age cache control doesn't work

Chrome doesn't cache images/js/css

like image 29
neallred Avatar answered Oct 06 '22 00:10

neallred