General Question

richardhenry's avatar

Can you use JavaScript to force-replace a particular image in the browser cache?

Asked by richardhenry (12664points) July 13th, 2008

If the referenced image has changed, it often takes a hard refresh of the page before the new copy appears. Can you use JavaScript to force the browser to replace that particular file in the cache, by loading the image in a particular way?

Observing members: 0 Composing members: 0

4 Answers

damien's avatar

I’m not sure about doing it in js but if you make it so that the image/stylesheet/javascript file in question has a unique string after the filename like ’/images/header.jpg?somerandomstring’, when you change that string, it should force browsers to re-cache.

I use the project’s current git revision to generate the string. That way, whenever you deploy a new version, it’ll have a new cache string.

richardhenry's avatar

That is a genius solution! I never thought of that. Thank you.

paulc's avatar

You could go one step further and just use the modified timestamp of the file so that you’re sure it only breaks the cache when the file actually changes rather than every page load or for every revision of a repository that might not affect the file.

richardhenry's avatar

These are great suggestions. Thank you.

Answer this question




to answer.

This question is in the General Section. Responses must be helpful and on-topic.

Your answer will be saved while you login or join.

Have a question? Ask Fluther!

What do you know more about?
Knowledge Networking @ Fluther