首页 > 解决方案 > LAMP 上的 serviceWorker 不存在

问题描述

我正在根据“ https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers ”测试 serviceWorker

我的电脑装有 Ubuntu 18.04、apache2 v2.4.29、firefox 69.0.1

我看到 serviceWorker 只在 HTTPS 域上运行,但为了测试,它也在端口 80 和 localhost 上运行。

我在我的一个测试域 outilsrouteur.test 上对其进行了测试,其中 /etc/apache2/sites-available/outilsrouteur.test.conf 是:

<VirtualHost *:80>
        ServerName outilsrouteur.test
        ServerAlias www.outilsrouteur.test
        DocumentRoot ***/siteoutilsrouteur  // *** = my real directory
        <Directory ***/siteoutilsrouteur> // *** = my real directory
            DirectoryIndex index.html index.php
            Options SymLinksIfOwnerMatch
            AllowOverride all
            Require local
        </Directory>
    ErrorLog ***/siteoutilsrouteur/error.log.txt
    CustomLog /access.log combined
</VirtualHost>

和 etc/apache2/port.conf 是:

Listen 80

我几乎没有将 app.js 修改为:

console.log("===================\napp.js :\n\n"); //****
if ('serviceWorker' in navigator) {
console.log("serviceWorker existe");
  navigator.serviceWorker.register('./sw-test/sw.js', {scope: './sw-test/'})
  .then((reg) => {
    // registration worked
    console.log('Registration succeeded. Scope is ' + reg.scope);
  }).catch((error) => {
    // registration failed
    console.log('Registration failed with ' + error);
  });
} else {  //****
console.log("serviceWorker n'existe pas / doesn't exist : '"+location.port+"'"); //****
}

我以为我的 LAMP 可以监听 80 端口,但是,当我调用我的测试页面(第一步)时:

<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width">
    <title>SW map</title>
  <body>
    <h1>Pour voir</h1>
    <section></section>
    <script src="app.js"></script>
  </body>
</html>

控制台显示此消息:

===================
app.js :

app.js:1:9
serviceWorker n'existe pas / doesn't exist : ''

所以,我的测试运行,但位置端口不是 80 并且不存在 serviceWorker ......

你能告诉我我的赌注吗?

标签: apache2portservice-workerlamp

解决方案


好的,在 mdn 页面的底部:

测试时,您可以通过检查 Firefox 开发人员工具设置中的“通过 HTTP 启用 Service Worker(当工具箱打开时)”选项来绕过 HTTPS 限制。

它确实起作用。


推荐阅读