ServiceWorker,不能在模块外使用导入语句
我有一个服务工作者的反应示例,带有导入语句。 像:
import { clientsClaim } from 'workbox-core';
如果我在服务工人中使用此代码(REECT),我会收到消息:
Uncaught SyntaxError: Cannot use import statement outside a module (at service-worker.js:1:1)
错误在哪里?
I have an react-example of a service worker, with import statements.
Like:
import { clientsClaim } from 'workbox-core';
If I use this code (react) in my service-worker, I get the message:
Uncaught SyntaxError: Cannot use import statement outside a module (at service-worker.js:1:1)
Where is the mistake?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要捆绑使用ES模块导入的服务工作者文件,因为它们是目前不支持所有服务工作者运行时环境。
workbox文档还有更多有关此用法的例子。任何支持ES模块的捆绑器都应起作用;我倾向于使用
eSbuild
如今。You need to bundle a service worker file that uses ES module imports, as they are not supported in all service worker runtime environments at the moment.
The Workbox documentation has some more examples of this usage. Any bundler that supports ES modules should work; I tend to use
esbuild
nowadays.