2

How can I force Google Chrome to download some .js/.css/images from local cache if remote file is not available (like if Chrome is configured to use Squid proxy, which will serve this file for me, even if cdn is not available).

For example, I load http://example.com which uses jquery library, located in CDN with address http://cdn-example.com/js/jquery.latest.js

Suddenly cdn-example.com becomes unavailable/respondes very slowly/respondes with 404 error/etc.

http://example.com is still available but is unusable because jquery is not loaded.

changer
  • 329
  • 2
  • 4
  • 19

1 Answers1

1

Is this actually happening? The point of a CDN is that it should be highly redundant and always available. If one host in the cluster goes down, your request will be routed to another that can serve the request.

I don't know of any possible way to do what you're asking, but if you're really concerned about it you can try something like this:

<html>
<head>
...
<script src="//cdn.com/js/jquery.latest.js"></script>
<script>window.jQuery || document.write('<script src="//otherplace.com/js/jquery.js"></script>')</script>

...
</head>

That tries to load it from the first URL, if that failed for one reason or another, the next script block checks to see if jQuery is defined (which it won't be on failure) and if so writes a new script tag to the document which triggers it to load from the fallback resource.

drew010
  • 68,777
  • 11
  • 134
  • 162
  • I do not control website which I am using, so I can not change it this way. I know about Developer tools and I can manually insert .js via console, but the question is about forcing download from cache if remote file is not available. – changer Jun 23 '16 at 06:57
  • I see, in that case I don't think so. You could either install an extension (like [this](https://chrome.google.com/webstore/detail/script-injector/fddnddnolonllcgfbenaloajnbhebmob?hl=en)) to do so, or edit your hosts file to point the CDN to your local computer and serve the JS from a local server. – drew010 Jun 23 '16 at 07:05