Search code examples
asp.net-corehttpcachingbrowser-cache

Cache response but always check for newer version


I have a http API that returns PDF file that is then shown in frame. The file is occasionally updated on the server.

How to tell the browser to always check whether there is a newer version on the server, but avoid downloading the cached version again?

So when requesting the file, the browser should include some timestamp or version number of currently cached file in some header. The server would respond with a newer version or with HTTP code 304 - Not Modified


Solution

  • Your response should include the following headers:

    • Cache-Control: no-cache
    • ETag: ...

    The Cache-Control tells the browser to keep the file in the cache but to check with the server each time to make sure the cached response is current.

    The ETag is used to identify the current version of the file. You can provide your own value (it could be a version number, say) or you can let your web framework handle it automatically (generally by hashing the contents of the response). I don't use ASP.NET but I assume there's a standard way of enabling this functionality.