如何在 chrome 中允许 serviceworkers 使用 http

How to allow serviceworkers for http in chrome

目前我只在我的本地主机上测试我的 ServiceWorker。 但问题是,如果我在 url 中将 'localhost' 更改为我的机器名称,它不允许 ServiceWorker 并提供香草 response.I 假设这是因为 http 不允许使用 serviceworker。

现在,如果我想用我的机器名向某人发送一个测试链接,我无法做到that.I想知道是否有任何方法允许 chrome 的 serviceworker。

因为您想向其他人发送 link,您需要通过 https 将您的服务器公开到 public Internet。你可以 set an https server with an autosigned certificate up but I think the simplest option is to use ngrok。安装 ngrok 后,启动本地服务器:

$ python -m SimpleHTTPServer

然后使用以下命令启动 ngrok:

$ ngrok http 8000

鉴于 8000 是 Python 服务器启动的端口,您将看到:

ngrok by @inconshreveable                                     (Ctrl+C to quit)

Tunnel Status                 online
Version                       2.0.19/2.0.20
Web Interface                 http://127.0.0.1:4040
Forwarding                    http://27223554.ngrok.io -> localhost:8000
Forwarding                    https://27223554.ngrok.io -> localhost:8000

Connections                   ttl     opn     rt1     rt5     p50     p90
                              0       0       0.00    0.00    0.00    0.00

现在分发 https://27223554.ngrok.io 作为 link 以测试您的网站。它会在您保持 ngrok 运行 并连接到 Internet 时工作。当然,你也可以自己使用link。

我不会说它是重复的,因为它更具体。

我 运行 进行了一些测试,我的建议是您应该将 Firefox 与 devtools.serviceWorkers.testing.enabled 一起用于您的客户

  • 如果你只使用 ngrok 对你没有帮助(尽管它是一个了不起的工具)
  • 如果你想使用 --unsafety-treat-insecure-origin-as-secure 你应该知道你不能将它用于 chrome,它不受支持。也许 canary 或 chronium 可以工作,但我没有检查。