navigator.serviceWorker.controller始终为空 [英] navigator.serviceWorker.controller is always null

查看:752
本文介绍了navigator.serviceWorker.controller始终为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到注册serviceWorker后navigator.serviceWorker.controller始终为空的问题。我从来没有强制刷新,只是刷新页面。我使用Google Chrome 42.0.2311.152 m(32位)来测试它。

  var currentServiceWorker = null; 
navigator.serviceWorker.register(SERVICE_WORKER_URL).then(function(serviceWorkerRegistration {
if(navigator.serviceWorker.controller){
currentServiceWorker = navigator.serviceWorker.controller;
} else {
currentServiceWorker = serviceWorkerRegistration.active;
}
});



<根据这个:

lockquote

ServiceWorkerContainer接口的控制器只读属性返回一个ServiceWorker对象,如果它的状态被激活(相同由ServiceWorkerRegistration.active返回的对象)。如果请求是强制刷新(Shift +刷新)或者没有活动工作者,则此属性返回null(来源: https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/controller ) p>

navigator.serviceWorker.controll er 应该返回与 serviceWorkerRegistration.active 相同的对象。但有了 .active 我得到了活动工作者, .controller 不是。



您对此有何看法?

谢谢,
Andi

解决方案

调试这件事时,我首先要看的是当前页面是否属于服务工作者的范围。如果您的当前页面不在该范围内,那么它将不受控制(但您注册的服务人员仍然可以被认为是活动的)。



您是调用注册(SERVICE_WORKER_URL),默认情况下,将使用包含您的服务工作者脚本的目录作为范围。这通常是你想要的,但你需要确保 SERVICE_WORKER_URL 引用位于网站根目录的脚本。这样,它就能够控制同一根级别的页面或Web根目录下的目录中的页面。因此,如果您的服务工作人员JavaScript文件当前位于 / scripts / 子目录或类似目录下,请将其移至根目录。



您可以通过访问 chrome:// serviceworker-internals / 来查看服务工作者的当前范围,并查看与您的注册相关的范围。 / p>

I have the problem that after registering the serviceWorker the navigator.serviceWorker.controller is always null. I never do a force refresh and just refresh the page. I test it with Google Chrome 42.0.2311.152 m (32-Bit).

var currentServiceWorker = null;
navigator.serviceWorker.register(SERVICE_WORKER_URL).then(function(serviceWorkerRegistration { 
  if (navigator.serviceWorker.controller) {
    currentServiceWorker = navigator.serviceWorker.controller;
  } else {
    currentServiceWorker = serviceWorkerRegistration.active;
  }
});

According to this:

The controller read-only property of the ServiceWorkerContainer interface returns a ServiceWorker object if its state is activated (the same object returned by ServiceWorkerRegistration.active). This property returns null if the request is a force refresh (Shift + refresh) or if there is no active worker. ( Source: https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/controller)

navigator.serviceWorker.controller should return the same object as serviceWorkerRegistration.active. But with .active I get the active worker, with .controller not.

Do you have any ideas for that situation?

Thank you, Andi

解决方案

The first thing I'd look at when debugging this is whether the current page you're on falls under the scope of the service worker. If your current page isn't under the scope, then it won't be controlled (but the service worker you registered can still be considered active).

You're calling register(SERVICE_WORKER_URL) which will, by default, use the directory that contains your service worker script as the scope. That's normally what you'd want, but you need to make sure that SERVICE_WORKER_URL refers to a script that's located at the root of your web site. That way, it will be able to control pages both at the same root level, or pages in directories underneath your web root. So if your service worker JavaScript file is currently under a /scripts/ subdirectory or something like that, move it up to the root.

You can check the current scope of your service worker by visiting chrome://serviceworker-internals/ and seeing what the scope associated with your registration is.

这篇关于navigator.serviceWorker.controller始终为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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