My websites use stylesheets and script ressources with the integrity
attribute (also known as SRI, subresource integrity) and have cache policies currently ranging up to a couple of weeks in some cases, for example cache-control: max-age=1209600
HTTP header for two weeks.
Examples of linking files in HTML:
<link rel="stylesheet" href="css/style.css?v=1.2.3" integrity="sha384-XELNiyGkz5oAOGcTFKLBFzEE6xu3y73Ler+dPW2m6G+rQMHECjLL6YoVd+tgQGCV" crossorigin="anonymous">
<script src="js/script.js?v=1.2.3" integrity="sha384-xSRUW2CepOugrp4wQm8hGlSp1vZ3YiWvJzh79+iozRyy/p//s5HSaOslz8KHqHzA" crossorigin="anonymous"></script>
Whenever a resource changes the ?v=<version>
URL parameter (and SRI hash of course) are updated accordingly in the referencing HTML files, expecting browsers to avoid using a cached version of the resource files and forcing it to request the files again from the server. This is common practice to force updates of cached files on browsers to my understanding.
This works as expected, except that it doesn't ... in some cases. Chrome, Firefox and Safari on desktop for example appear to behave like intended, however at least the mobile version of Chrome doesn't seem to care. This results in users visiting a website once and again a second time (within the cache times of the changed files in question) stylesheets and script resources will be ignored, resulting in a bad user experience. The browser fetches the HTML file, doesn't reload the cached (but now changed) resource files and as a consequence of course fails the integrity check with the new hash. Once manually deleting the cache everything works again, until the next update rolls out ...
Is there any way to avoid this problem that doesn't involve changing the file name or path directly? In theory I could replace css/style.css?v=1.2.3
with something like css/style-v1.2.3.css
or css/v1.2.3/style.css
but I'd rather not. Also disabling the cache policies is not an option.
Okay, apparently query parameters are known to cause problems and are not a reliable option here. Changing the filename or path are the recommended and reliable ways to solve this issue.