在Google Chrome中强制从本地缓存中下载 [英] Force download from local cache in Google Chrome

查看:369
本文介绍了在Google Chrome中强制从本地缓存中下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果远程文件不可用,我该如何强制Google Chrome从本地缓存中下载一些.js / .css /图像(例如,如果Chrome配置为使用Squid代理,它将为我提供此文件,即使cdn不可用)。



例如,我加载 http://示例。它使用jQuery库,位于CDN,地址为 http:// cdn- example.com / js / jquery.latest.js



突然cdn-example.com变得不可用/ responds非常缓慢/ 404错误响应/ etc。



http://example.com 仍然可用,但是因jquery未加载而无法使用。

解决方案

这是否真的发生? CDN的要点是它应该是高度冗余的并且始终可用。如果群集中的一台主机发生故障,您的请求将被路由到另一台可以处理该请求的请求。



我不知道有什么方法可以做到'如果你真的关心它,你可以尝试这样的事情:

 < 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>

尝试从第一个URL加载它,如果因为某种原因失败,脚本块检查是否定义了jQuery(它不会失败),如果是,则向文档写入一个新的脚本标记,以便触发它从后备资源加载。


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.

解决方案

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.

这篇关于在Google Chrome中强制从本地缓存中下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆