title: Dynamic <img> tags unnecessarily reloading static images
body: <p>I already asked this question at <a href="https://stackoverflow.com/questions/40840600/">&lt;img&gt; tag not respecting max-age, and unnecessarily reloading images</a> and thought the comments provided a satisfactory answer, but turns out I was mistaken. In the six months since, I think I've thought up a solution, but want to check it out here before implementing it. Will it work? Is it worth implementing? Any simpler solution? etc.</p>
  
  <p>To briefly re-describe the problem, I have a cgi program that generates gif images for html pages with tags like the following</p>
  
  <pre><code>&lt;img src="http://example.com/cgi-bin/gifprogram.cgi?query_string"&gt;
  </code></pre>
  
  <p>The <strong>?query_string</strong> specifies the image to be generated, and once the program generates it, the image is cached in a <strong>cachedirectory/<em>filename</em>.gif</strong> with <strong><em>filename</em></strong> chosen as the md5-hash of the <strong>?query_string</strong>. Then subsequent tags calling for the same image just get that same cached file.</p>
  
  <p>So browsers can use their own cache, but treat the tag as dynamic, and re-download the same, often-large, images again-and-again, every time they see the tag, regardless of max-age or any other http-header info (that I could think to try). Okay, I understand why "dynamic" is necessary for actually-dynamic cgi applications. But there ought to be a way to tell the browser "static". There doesn't seem to be any standard way, so here's my kludge...</p>
  
  <p>On the server side, I'm going to write a short cgi program called <strong>checkcache.cgi?<em>expression</em></strong> that checks the cache directory for a filename that's <strong>md5hash(<em>expression</em>)</strong>, i.e., it will check whether or not a desired image is already cached. And its output to stdout will be the usual http-headers, ending with text/text, followed by either the full <strong>path/filename.ext</strong> to the cached file corresponding to <strong><em>expression</em></strong>, or "notfound" if the desired image doesn't yet exist.</p>
  
  <p>On the client side, rather than directly writing &lt;img&gt; tags, I'll write a php script that takes the desired <strong><em>expression</em></strong> and does the following. First, it uses curl to curl_exec() checkcache.cgi. Then, if the cached file already exists, it echo's a <strong>static</strong> &lt;img&gt; tag, with <strong>src="http://etc/filename.gif"</strong>, using the filename returned by checkcache. But if checkcache returns "not found", then it echo's the <strong>dynamic</strong> tag to run the gifprogram.cgi dynamically.</p>
  
  <p>Since checkcache only emits a few dozen bytes (rather than some of these gifs which can sometimes run half a MB or more), it should go real quick. So, my question again -- the above strategy will work, right? Or what's wrong with it? Or can you suggest a simpler idea? Thanks.</p>
  
  <p><strong>-----------------------------------------------------------------</strong><br>
  <strong>Edit</strong> Update: Yeah, that works. Finally. Just an awfully elaborate way of accomplishing what I'd think ought to be a simple task. And I'd still be interested to learn some simpler procedure.</p>