0

我正在使用sw预缓存来管理我的服务人员。让我们说我的网站是abc.com,服务人员正在这个网站上运行,并通过url加载abc.com/service-worker.jssw预缓存 - 从不同域加载的资产的运行时缓存

所有的css和js都是从​​不同的网址加载来优化目的我们说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' 
    } 
    } 
} 

这只能从网址abc.com/style/style.css缓存CSS和不xyz.com/style/test.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\//, 
    } 
} 

但都是徒劳的。如果有人遇到同样的问题,任何帮助正确的方向将不胜感激。

回答

1

我相信你的问题是你传递一个字符串作为urlPattern,当你实际上应该传递一个正则表达式。

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