单独缓存组合的javascript文件

单独缓存组合的javascript文件,javascript,http,caching,cross-browser,browser-cache,Javascript,Http,Caching,Cross Browser,Browser Cache,即使将JavaScript文件合并到单个JavaScript文件中,也可以单独缓存它们吗 我之所以需要它,是因为我希望既能缓存下载的内容,又能结合通过internet发送的数据 浏览器特定的解决方案也将非常感谢 是的,Angular实际上也做了类似的事情。诀窍是缓存在不同的级别,而不是HTTP协议级别。您可以自己将其缓存到localStorage或indexeddb 创建一个缓存对象,无论何时添加一个文件,都将其放在那里 // a.js, this file generated by a rea

即使将JavaScript文件合并到单个JavaScript文件中,也可以单独缓存它们吗

我之所以需要它,是因为我希望既能缓存下载的内容,又能结合通过internet发送的数据


浏览器特定的解决方案也将非常感谢

是的,Angular实际上也做了类似的事情。诀窍是缓存在不同的级别,而不是HTTP协议级别。您可以自己将其缓存到
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";
一个单文件请求可以简单地包含这四行代码,这四行代码对于要缓存的每个文件都是重复的,版本控制在这里也可能是合适的

接下来,在身体部分的末尾,你可以

// 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"]);
看一看。特别是大口喝海螺

此代码读取所有my.js源文件:

  // Make sources.  Uglify, Concat, and add hash if necessary
  // Determine output file.  Only used if config.concat === true
  var destJs = 'app' + (config.minify?'.min':'') + '.js';
  var appJs = gulp.src([config.dashboard.js, '!/src/dashboard/vendor/'])
  .pipe(plugins.plumber())
  .pipe(plugins.ngAnnotate())
  .pipe(plugins.angularFilesort())
  .pipe(plugins.if(config.minify, plugins.uglify()))
  .pipe(plugins.if(config.concat, plugins.concat(destJs)))
  .pipe(plugins.if(config.hash, plugins.hash()))
  .pipe(gulp.dest(config.dashboard.dest + '/js'))
  • ngAnnotate()注释angular的依赖注入
  • angularFilesort()按依赖项对它们进行排序
  • plugins.if()有条件地使用plugins.uglify()丑化它们
  • plugins.if()有条件地与plugins.concat()连接
  • plugins.if()有条件地使用plugins.hash()对它们进行散列,以破坏缓存
  • 然后将所有文件(或仅一个)写入我的项目
    build/js
    文件夹
var plugins=require('gulp-load-plugins')()加载插件

这里有很多,但一旦你进入它,它真的很简单。根据我的经验,这比咕哝或制造更容易

对于您的问题,您可以在管道()的多个位置吞咽.dest()。缩小/连接前后