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/