I've been stuck in caching hell for the past couple of days and while I'm starting to get it still struggling a little bit.
Desired Result
I want to include a JS file on a site and have it cached and/or only fetch a new copy of the file when the file has been changed on the server. I DO CANNOT use version tags or hashes in the file name. The file name needs to stay consistent.
Where I'm at
From what I understand after reading up on this using Etags are the perfect solution for this. They will update when the file is changed and will send the new version of the file if tags don't match. And this seems to work when I request the file via the browser
So you can see that on first request i get a 200 and download size is 292 B and on second request because tags match I get a 304 and download size is 137 B (Just header size I assume). Great! This is exactly what I want.
My request header with looks for the etag:
My Problem
Now this works when I request the file from the browser, but adding a js file in a script tag doesn't send request headers the same way. So, opening the following html file
<!DOCTYPE html>
<html>
<body>
<script src="https://myTestSite.com/testCache.js">
</script>
</body>
</html>
My js file is fetched every time because the if-none-match
request header is not present. How do I replicate the behavior that was observed in the above scenario when using html script tag?
TL;DR
How do I use Etags to request only modified JS files when using html script tags and not simple browser GET request?