Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cache background image

Is there a way to "cache" background image.

For example.. Background image is 3x3px and it's set like this:

body {
    background: #000 url(bg.png);
}

When refresh happens, background image "flickers" for second.

Is there a cross-browser solution? (for Apache/PHP server if that is relevant)


If you go to seo.hr and browse navigation,... you can see what I'm trying to do.

http://www.seo.hr/

http://www.seo.hr/usluge/izrada-stranica

http://www.seo.hr/usluge/optimizacija-za-trazilice

like image 643
enloz Avatar asked Dec 13 '11 23:12

enloz


People also ask

What is cache in images?

An image service cache represents a snapshot of your image service at one point in time, with one or three bands, and using a specific mosaic method.

Should images be cached?

Generally yes, images should be cached in at least memory, and depending on your app (how likely is it to be reused,etc) in memory and storage. If you want to support your 3rd point (displaying when offline), you need to do storage caching, and memory caching is optional but probably a good idea.

Do browsers automatically cache images?

A browser or Web cache does exactly that, except with program and website assets. When you visit a website, your browser takes pieces of the page and stores them on your computer's hard drive. Some of the assets your browser will store are: Images - logos, pictures, backgrounds, etc.

What is background image tiles?

A tiled background image is a small size image that is designed so that it can repeat seamlessly both horizontally and vertically.


1 Answers

I think you need to determine first if the issue actually is a caching issue or if it's caused by the size of your image. You could use a program like Wireshark or Fiddler to do this, but to be honest it's overkill for your need and you probably already have a browser with developer tools.

Here's how you determine where an image is coming from in Chrome (the other browsers are similar).

  1. Open your developer tools and go to the "Network" tab.
  2. Find "bg.png" in the list of network requests and click on it's name. Below is an example of having selected a stack overflow image from this page.

Google Chrome Image Retrieved from Cache

Notice that it says status 200 (from cache). The browser didn't need to go out to the server and rerequire that resource. It used the cache. If that "from cache" text wasn't there it wasn't reusing cached resources.

There is also the potential that you'll get a status code of 304. That means that the server said the image wasn't modified since the last request that you made. You do make the server trip in that case.

Ok, so my image wasn't in cache... now what?

There are a few reasons that this could occur.

  1. You're request headers aren't set to tell the browser to cache the image (also found in that same "Headers" tab that you would have seen that Status Code if the browser actually went to the server for the image). You'll want to set cache-control and expires to something that makes sense for you. Cache headers can get a bit complicated you may want to browse through this caching tutorial document.
  2. Is it SSL? If so not all browsers cache this but most modern browsers do. Set cache-control: public on these images (and also expires).

The real question here is how do you fix this? Unfortunately, that's entirely dependent on the server and/or the framework that you are using. As the OP is using Apache, they can find great documentation on the Apache module mod_expires to figure out how to tweak caching for their site.

like image 157
scottheckel Avatar answered Oct 07 '22 23:10

scottheckel