通过 HTTP 测试 Service Worker 的选项 [英] Options for testing service workers via HTTP

查看:25
本文介绍了通过 HTTP 测试 Service Worker 的选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测试 Service Worker,但我有一个虚拟主机设置,我似乎无法在本地主机上启用 https.

I want to test service workers but I have a virtual host setup and I can't seem to be able to enable https on localhost.

如何将本地虚拟主机 URL 列入白名单以测试 Service Worker每当我尝试在本地主机上注册 Service Worker 时?Chrome 表示需要 https 才能启用 Service Worker.我怎样才能至少在本地测试中突破这个限制.

How can I whitelist my local virtual host url to test service workers whenever I try and register for the service worker on the localhost? Chrome says https is required to enable service worker. How can I get past this restriction at least for local testing.

推荐答案

一般来说,为了使用 Service Worker,您需要通过 HTTPS 为您的页面和 Service Worker 脚本提供服务.Prefer Secure Origins For强大的新功能.

In general, you need to serve both your page and your service worker script via HTTPS in order to use service workers. The rationale is described at Prefer Secure Origins For Powerful New Features.

HTTPS 要求有一个例外,以促进本地开发:如果您通过 http://localhost[:port] 或通过 http 访问您的页面和 Service Worker 脚本://127.xyz[:port],则无需任何进一步操作即可启用 Service Worker.

There's an exception to the HTTPS requirement in place to facilitate local development: if you access your page and service worker script via http://localhost[:port], or via http://127.x.y.z[:port], then service workers should be enabled without any further actions.

在最新版本的 Chrome 中,您可以在本地开发期间通过 chrome://flags/#unsafely-treat-insecure-origin-as-secure如本答案所述.

In recent versions of Chrome, you can work around this requirement duriing local development via chrome://flags/#unsafely-treat-insecure-origin-as-secure, as explained in this answer.

Firefox 通过 devtools 提供类似的功能.serviceWorkers.testing.enabled 设置.

Firefox offers similar functionality, via the devtools.serviceWorkers.testing.enabled setting.

请注意,此功能旨在促进原本无法进行的测试,并且在为网站的生产版本提供服务时,您应该始终计划使用 HTTPS.不要让真实用户完成启用这些标志的步骤!

Please note that this functionality is only meant to facilitate testing that wouldn't otherwise be able to take place, and you should always plan on using HTTPS when serving the production version of your site. Don't ask real users to go through the steps of enabling those flags!

这篇关于通过 HTTP 测试 Service Worker 的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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