Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is actually rather simple to accomplish. Make an AJAX request for the image with the <code>Cache-Control</code> header set to <code>max-age=0</code>.</p> <p>The server must respond with either the updated image (and a new set of cache headers), or with a 304 <strong>containing a <code>Cache-Control</code> header</strong> like <code>public, max-age=123</code>.</p> <p>The 304 with a <code>Cache-Control</code> header is very important because it instructs the browser to update the freshness information associated with the resource.</p> <p>When the request completes, remove the old <code>&lt;img&gt;</code> element from the DOM and reinsert a new one pointing to the same <code>src</code>.</p> <p>This works because the <em>request</em> <code>Cache-Control</code> header tells the browser to ignore its cache and go to the network. When the browser gets a response back, it updates its cache &ndash; even though you don't actually do anything with the AJAX results.</p> <p>Now when you remove the old element and insert a new one, the browser grabs the recently updated image directly from its cache.</p> <p>In Chrome's network inspector, you see two requests: the AJAX request, then the request generated by reinserting the <code>&lt;img&gt;</code> element. However, if the server has set the <code>Cache-Control</code> headers properly, the second "request" is fulfilled by the browser's cache, and nothing is actually sent over the wire.</p> <pre><code>function updateImage(el) { el = $(el); var src = el.attr('src'); $.ajax({ url: src, headers: { 'Cache-Control': 'max-age=0' }, success: function() { var parent = el.parent(); el.remove(); // this assumes that the &lt;img&gt; has no siblings and no attributes/classes/etc... parent.append('&lt;img&gt;').attr('src',src); } }); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload