如何使用内联服务工作者 [英] How to use inline service worker

查看:111
本文介绍了如何使用内联服务工作者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道你需要为服务工作者创建一个单独的文件并注册它。如何在同一个文件中编写服务工作者代码并使用它?

I know that you need to create a separate file for service worker and register it. How can i write the code of service worker in the same file and use it ?

我知道我可以使用 Blob 在网络工作者中。服务工作者是否相同?

I know that I can use Blob in web workers . Is it same for service Workers ?

推荐答案

根据MDN 您必须指定URL路径。我可以想象如何内联的唯一方法是使用数据URI ,但是当您在Chrome中尝试以下操作时:

According to MDN you must specify the URL path. The only way I can imagine how to do that inline is using data URIs, but when you try the following in Chrome:

navigator.serviceWorker.register("data:application/javascript,alert('123');");


未捕获(在承诺中)DOMException:无法注册
ServiceWorker:提供的scriptURL('null')的来源与
不匹配当前原点('我的原始URL')。

Uncaught (in promise) DOMException: Failed to register a ServiceWorker: The origin of the provided scriptURL ('null') does not match the current origin ('my origin URL').

所以我宁愿说无法做到这一点。

So I would rather say no way to do that.

关注 Blob 查看关注表示你所说的相同

Concerning Blob check the following which says the same you've told


Blob在工作人员范围内可用。

Blob is available in Worker scopes.

它没有命名工人类型。如果您查看官方w3c草案,他们没有关于blob的一个单词。

It does not name the workers type. If you check the official w3c draft they don't have a single word about blob in there.

你能知道的唯一方法 - 自己试试Chrome。但即使它现在可以在Chrome中使用,但这并不意味着它会在以后工作。它仍然是一项实验性技术。

The only way you can know it - try in Chrome yourself. But still even if it will work out for you in Chrome now it does not mean it will work later. It is still an experimental technology.

这篇关于如何使用内联服务工作者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆