是否可以单独缓存 JavaScript 文件,即使它们组合成单个 JavaScript 文件?
我需要这个,因为我希望同时拥有缓存下载内容和组合通过互联网发送的数据的优势。
浏览器特定的解决方案也将不胜感激!
最佳答案
是的,Angular 实际上做了类似的事情。诀窍是在不同的级别缓存,而不是 HTTP 协议(protocol)级别。您自己将其缓存到 localStorage
或 indexeddb。
创建一个缓存对象,每当你添加一个文件,就把它放在那里
// a.js, this file generated by a readFile call in nodejs or whatever
// build process you want to have
var cache = cache || {}; // might also want to use an array here to preserve order
cache["a.js"] = "your real a.js file as code";
一个文件请求可以简单地包含为您要缓存的每个文件重复的这四行,版本控制在这里也可能是合适的。
接下来,在你的 body 部分的末尾,你可以放
// let's say you need in this particular page a.js b.js
eval(cache["a.js"]); // write a helper function for this, remember
eval(cache["b.js"]); // cache is for example localStorage with a prefix
eval(cache["c.js"]);
关于javascript - 单独缓存组合的 javascript 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23808543/