Can you use JavaScript to force-replace a particular image in the browser cache?
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
Answers
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.
That is a genius solution! I never thought of that. Thank you.
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.
These are great suggestions. Thank you.
Answer this question 