首页 > 解决方案 > workbox.cacheableResponse.Plugin 不抛出构造函数错误,v6.0.2

问题描述

importScripts('https://storage.googleapis.com/workbox-cdn/releases/6.0.2/workbox-sw.js');


workbox.routing.registerRoute(
  ({ url, request }) => {
    if (url.host === self.location.host && request.destination !== 'document') {
      return true;
    }
  },
  new workbox.strategies.CacheFirst({
    cacheName: CACHE_KEY,
    plugins: [
      new workbox.cacheableResponse.Plugin({
        statuses: [0, 200]
      })
    ]
  })
);

抛出错误: Uncaught TypeError: workbox.cacheableResponse.Plugin is not a constructor。

但是如果我使用new workbox.cacheableResponse.CacheableResponse({statuses: [0, 200]}),没有错误,但是插件仍然没有效果。(即使将可缓存状态设置为 [0,200],不透明响应也不会存储到缓存中)

标签: workbox

解决方案


新的 workbox.cacheableResponse.Plugin 是 v4.0.3 的 api。我使用v6.0.2,但不幸的是@types/cacheableResponse 的最新版本是v4.0.3。
正确的 api 是 v6.0.2 的新 workbox.cacheableResponse.CacheableResponsePlugin


推荐阅读