Workbox 5 syntax error - Uncaught TypeError: workbox.expiration.CacheableResponsePlugin is not a constructor

Workbox 5 syntax error - Uncaught TypeError: workbox.expiration.CacheableResponsePlugin is not a constructor

我正在尝试为小型静态站点设置一个简单的 Service Worker,但我收到 Service Worker 控制台错误:

sw.js:59 Uncaught TypeError: workbox.expiration.CacheableResponsePlugin is not a constructor

这是在行 new workbox.expiration.CacheableResponsePlugin

如有任何关于如何解决此问题的建议,我们将不胜感激。

  workbox.routing.registerRoute(
    /\.(?:html)$/,
    new workbox.strategies.NetworkFirst({
      cacheName: 'html-cache',
      plugins: [
        new workbox.expiration.CacheableResponsePlugin({
          statuses: [0, 200],
        }),

        new workbox.expiration.ExpirationPlugin({
          maxEntries: 50,
          maxAgeSeconds: 5 * 60,
        })
      ]
    })
  )

我从 workbox v4 迁移到 v5 时遇到了同样的问题。

在您的代码中,

new workbox.expiration.CacheableResponsePlugin

应该是

new workbox.cacheableResponse.CacheableResponsePlugin
Example: 

workbox.routing.registerRoute(new RegExp('https://assets.abcd.com/.*\.*'),
new workbox.strategies.CacheFirst({
cacheName: 'assets',
plugins: [
  new workbox.cacheableResponse.Plugin({
     statuses: [0, 200, 206] // 206 Partial Code. 
  }),
  new workbox.rangeRequests.Plugin()
]}))

new workbox.cacheableResponse.Plugin

现在