javascript - sw 预缓存 - 从不同域加载 Assets 的运行时缓存

标签 javascript service-worker progressive-web-apps sw-precache sw-toolbox

我正在使用 sw 预缓存来管理我的服务 worker 。假设我的站点是 abc.com 并且 service worker 正在该站点上运行并由 url abc.com/service-worker.js 加载

为了优化目的,所有的 css 和 js 都从不同的 url 加载让我们说 xyz.com

当我提供时现在在运行时缓存中

{
  // See https://github.com/GoogleChrome/sw-toolbox#methods
  urlPattern: '*.css',
  handler: 'fastest',
  // See https://github.com/GoogleChrome/sw-toolbox#options
  options: {
    cache: {
      maxEntries: 20,
      name: 'css-cache'
    }
  }
}

这只缓存来自 url abc.com/style/style.css 而不是 xyz.com/style/test.css 的 css

我试过了

{
  // See https://github.com/GoogleChrome/sw-toolbox#methods
  urlPattern: 'https:\/\/xyz\.com\/*.js',
  handler: 'fastest',
  // See https://github.com/GoogleChrome/sw-toolbox#options
  options: {
    cache: {
      maxEntries: 20,
      name: 'js-cache'
    },
    // urlPattern: /\.googleapis\.com\//,
  }
}

但一切都是徒劳。如果有人遇到过相同的问题,我们将不胜感激在正确方向上提供的任何帮助。

最佳答案

我认为您的问题在于,当您实际上应该传递正则表达式时,却将字符串作为 urlPattern 传递。

因此,您应该使用 /https:\/\/xyz\.com\/* 而不是 'https:\/\/xyz\.com\/*.js' .js/.

关于javascript - sw 预缓存 - 从不同域加载 Assets 的运行时缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46444755/

相关文章:

javascript - 将 redis 与 node.js 一起使用时无法获取有效列表

javascript - 可撤销 promise 的状态

javascript - 这两个笨蛋有什么区别? (Angularjs,Angular-UI)

javascript - 如何在 Web 应用程序上获取键盘高度

python - Heroku 应用程序 : ModuleNotFoundError: No module named 'pwa'

svg - PWA InkCanvas 在 javascript 中将笔画保存为 svg

javascript - VueJS Axios JSON 切片/过滤器不是函数

google-chrome - Chrome 中服务工作线程的 net::ERR_CONNECTION_RESET

service-worker - 从 Service Worker 中读取 cookie?

ajax - 如何在 Service Worker Fetch API 中检测 URL 请求是否为 ajax