In an web pure vanilla JavaScript app that does not use service workers, I would like to explicitly cache a JavaScript file that is sitting on an AWS S3 file server. The following script would be sitting in the index.html file for the application (I’ve modified the URL as it's a client project):
<script>
caches.match('https://s3.amazonaws.com/com.myproject/myjavascript.js')
.then(function(response) {
if (response) {
return response;
} else {
fetch('https://s3.amazonaws.com/com.myproject/myjavascript.js')
.then(function(res) {
return caches.open('mycache')
.then(function(cache) {
cache.put('https://s3.amazonaws.com/com.myproject/myjavascript.js',res.clone());
console.log(res.clone());
return res;
});
});
}
});
</script>
I believe this code should do the following: Check if the myjavascript.js file is in the cache. If it is, return the JavaScript file which would then be executed by the browser. If myjavascriptfile.js is not found in the cache, it will be fetched and placed in the subcache ‘mycache’ and finally returned to the browser where it would be executed.
After running this, I find the URL for the file in the cache with a response of “Ok”, but the code is not executed by the browser and I don’t see the file contents in sources within the Chrome browser developer tools.
Why would this not be working? What is wrong with my thinking on this.
Many thanks, Fred