0

I am loading an images from server, and I am serving images from server like below

res.setHeader('Cache-Control', 'public, max-age=3600');  
res.writeHead('200', {'Content-Type': 'image/png'});
res.end(data,'binary');

I am calling image like below in image tag

<img src="http://server.com/image1" />

But when image1 value is changed at server how to load image from server not from cache. What are the necessary modifications I have to do for this. Please help me on this.

redV
  • 644
  • 1
  • 9
  • 25
  • possible duplicate of [Making sure a web page is not cached, across all browsers](http://stackoverflow.com/questions/49547/making-sure-a-web-page-is-not-cached-across-all-browsers) – Tim Cooper May 23 '14 at 11:31
  • @TimCooper There they do not want to cache, here I want to cache but want to revalidate when changed at server. There is lot of difference – redV May 23 '14 at 11:39

1 Answers1

0

You can do this by changing the following line for your header.

res.setHeader('Cache-Control', 'public, max-age=0, no-cache');  

The question is if you actually want to do this. This will result in every client connecting to your webserver getting that image again. It only makes sense if the image is really changing constantly.

mgerstner
  • 480
  • 1
  • 5
  • 21
  • I do not want to load image from server every time, want to load again when changed at server. I want to cache image from reload when changed at server. You solution will not cache an image at client, so it will reload every time – redV May 23 '14 at 11:43
  • Okay this would mean that the server needs to broadcast to every client that there has been a change on the data. As the client does not realize this, you need to use something like Server-sent events (SSE) or WebSockets to broadcast such a change to all clients. A workaround would be to query the server at a certain interval you define on the client side. – mgerstner May 23 '14 at 11:47