these instructions上添加了“添加到主屏幕”功能之后,我必须注册一个ServiceWorker。不幸的是,我无法使它正常工作。 Chrome记录两个错误:

我注册ServiceWorker的代码是:

if ('serviceWorker' in navigator) {
  navigator.serviceWorker.register('sw.js').then(function(reg){
    console.log("ServiceWorker installed.");
  }).catch(function(err) {
    console.log("ServiceWorker not installed: ", err)
  });
}
我不知道为什么Chrome认为这是x-js脚本。该代码位于index.php文件的底部,然后关闭</html>标记。
有谁知道,为什么这行不通?

最佳答案

您应该将MIME类型text / x-js添加到您的服务器,我认为这是apache。

https://webplatform.github.io/docs/tutorials/configuring_mimetypes_on_the_server/

10-06 00:28