简体   繁体   English

sw precache-从不同域加载资产的运行时缓存

[英]sw precache - runtime caching for assets loading from different domain

I am using sw precache to manage my service worker. 我正在使用sw precache管理我的服务人员。 Let us say my site is abc.com and the service worker is running on this site and loaded by url abc.com/service-worker.js 假设我的网站是abc.com,服务工作者正在此网站上运行并由url abc.com/service-worker.js加载

All the css and js are being loaded from different url for optimization purpose let us say xyz.com 为了优化目的,从不同的URL加载了所有的CSS和JS,让我们说xyz.com

Now in runtime cache when I provide 现在在运行时缓存中,当我提供

{
  // 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'
    }
  }
}

This caches css only from url abc.com/style/style.css and not xyz.com/style/test.css 这仅从URL abc.com/style/style.css缓存CSS,而不从xyz.com/style/test.css缓存CSS

I have tried 我努力了

{
  // 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\//,
  }
}

but all in vain. 但都是徒劳的。 If anyone has faced the same issue ever any help in the correct direction will be highly appreciated. 如果有人遇到过同样的问题,那么朝着正确方向的任何帮助将不胜感激。

I believe your problem is that you pass a string as urlPattern when you should actually pass a regular expression. 我相信您的问题是,您实际上应该传递正则表达式时将字符串作为urlPattern传递。

So instead of 'https:\\/\\/xyz\\.com\\/*.js' you should use /https:\\/\\/xyz\\.com\\/*.js/ . 因此,您应该使用/https:\\/\\/xyz\\.com\\/*.js/而不是'https:\\/\\/xyz\\.com\\/*.js' /https:\\/\\/xyz\\.com\\/*.js/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM