google chrome - navigator.serviceWorker.controller is always null -


i have problem after registering serviceworker navigator.serviceworker.controller null. never force refresh , refresh page. test 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 this:

the controller read-only property of serviceworkercontainer interface returns serviceworker object if state activated (the same object returned serviceworkerregistration.active). property returns null if request force refresh (shift + refresh) or if there no active worker. ( source: https://developer.mozilla.org/en-us/docs/web/api/serviceworkercontainer/controller)

navigator.serviceworker.controller should return same object serviceworkerregistration.active. .active active worker, .controller not.

do have ideas situation?

thank you, andi

the first thing i'd @ when debugging whether current page you're on falls under scope of service worker. if current page isn't under scope, won't controlled (but service worker registered can still considered active).

you're calling register(service_worker_url) will, default, use directory contains service worker script scope. that's you'd want, need make sure service_worker_url refers script that's located @ root of web site. way, able control pages both @ same root level, or pages in directories underneath web root. if service worker javascript file under /scripts/ subdirectory or that, move root.

you can check current scope of service worker visiting chrome://serviceworker-internals/ , seeing scope associated registration is.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -