简体   繁体   English

WorkboxSW 构造函数抛出错误未定义

[英]WorkboxSW constructor throws error Undefined

In my service worker file, I am using this code:在我的服务工作者文件中,我正在使用以下代码:

importScripts("https://storage.googleapis.com/workbox-cdn/releases/3.0.0-beta.2/workbox-sw.js");
const workbox = new WorkboxSW();

But I am getting error Uncaught ReferenceError: WorkboxSW is not defined hence my service worker is not registering.但是我收到错误Uncaught ReferenceError: WorkboxSW is not defined因此我的服务人员没有注册。

In Workbox v3, you won't normally explicitly construct a WorkboxSW instance. 在Workbox v3中,通常不会显式构造WorkboxSW实例。 Instead, there's a workbox variable exposed in the global scope that provides the interface to the library. 而是在全局范围内公开了一个工作workbox变量,该变量提供了库的接口。

Here's an example, excepted from the migration guide : 这是一个示例,但迁移指南除外:

Previously in v2: 先前在v2中:

importScripts('<path to workbox-sw>/importScripts/workbox-sw.prod.v2.1.3.js');

const workbox = new WorkboxSW({
  skipWaiting: true,
  clientsClaim: true,
  // etc.
});

workbox.router.registerRoute(...);

In v3, you just have to import the workbox-sw.js script, and a ready-to-use instance will be automatically available in the global namespace as workbox: 在v3中,您只需要导入workbox-sw.js脚本,即可在全局名称空间中自动将一个现成的实例用作workbox:

importScripts('<path to workbox-sw>/3.0.0/workbox-sw.js');

// workbox is implicitly created and ready for use.
workbox.routing.registerRoute(...);

I faced a similar issue but in my case, my sw was registering despite the error message.我遇到了类似的问题,但就我而言,尽管出现错误消息,我的 sw 仍在注册。 Adding type="javascript/worker" like this: <script type="javascript/worker" src="sw.js"></script> at my entry point, removed the error in dev tools.像这样添加 type="javascript/worker" : <script type="javascript/worker" src="sw.js"></script>在我的入口点,删除了开发工具中的错误。

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

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