navigator.serviceWorker.controller 在 serviceWorker.js 的 Create React App 中的作用
Role of navigator.serviceWorker.controller in serviceWorker.js of Create React App
以下是 CRA 生成的 serviceWorker.js 的摘录。
里面有if (navigator.serviceWorker.controller)
有什么意义
if (installingWorker.state === 'installed')
?
新的service worker已经安装成功。 'else' 部分是否处理尚未安装以前的 service worker 的场景?
navigator.serviceWorker
.register(swUrl)
.then(registration => {
registration.onupdatefound = () => {
const installingWorker = registration.installing;
if (installingWorker == null) {
return;
}
installingWorker.onstatechange = () => {
if (installingWorker.state === 'installed') {
if (navigator.serviceWorker.controller) {
// At this point, the updated precached content has been fetched,
// but the previous service worker will still serve the older
// content until all client tabs are closed.
console.log(
'New content is available and will be used when all ' +
'tabs for this page are closed.'
);
// Execute callback
if (config && config.onUpdate) {
config.onUpdate(registration);
}
} else {
// At this point, everything has been precached.
// It's the perfect time to display a
// "Content is cached for offline use." message.
console.log('Content is cached for offline use.');
// Execute callback
if (config && config.onSuccess) {
config.onSuccess(registration);
}
}
}
};
};
})
.catch(error => {
console.error('Error during service worker registration:', error);
});
navigator.serviceWorker.controller
will be set to a truthy value (a ServiceWorker
object) 如果当前页面在 service worker 的控制之下。
如果这是第一次注册 scope
包含当前页面的 URL 的服务工作者,则不会设置 navigator.serviceWorker.controller
(除非 service worker 自己调用 clients.claim()
).
如果这是对已注册 service worker scope
下的页面的后续访问,则将设置 navigator.serviceWorker.controller
,并且该页面将在 service worker 的控制下。
以下是 CRA 生成的 serviceWorker.js 的摘录。
里面有if (navigator.serviceWorker.controller)
有什么意义
if (installingWorker.state === 'installed')
?
新的service worker已经安装成功。 'else' 部分是否处理尚未安装以前的 service worker 的场景?
navigator.serviceWorker
.register(swUrl)
.then(registration => {
registration.onupdatefound = () => {
const installingWorker = registration.installing;
if (installingWorker == null) {
return;
}
installingWorker.onstatechange = () => {
if (installingWorker.state === 'installed') {
if (navigator.serviceWorker.controller) {
// At this point, the updated precached content has been fetched,
// but the previous service worker will still serve the older
// content until all client tabs are closed.
console.log(
'New content is available and will be used when all ' +
'tabs for this page are closed.'
);
// Execute callback
if (config && config.onUpdate) {
config.onUpdate(registration);
}
} else {
// At this point, everything has been precached.
// It's the perfect time to display a
// "Content is cached for offline use." message.
console.log('Content is cached for offline use.');
// Execute callback
if (config && config.onSuccess) {
config.onSuccess(registration);
}
}
}
};
};
})
.catch(error => {
console.error('Error during service worker registration:', error);
});
navigator.serviceWorker.controller
will be set to a truthy value (a ServiceWorker
object) 如果当前页面在 service worker 的控制之下。
如果这是第一次注册 scope
包含当前页面的 URL 的服务工作者,则不会设置 navigator.serviceWorker.controller
(除非 service worker 自己调用 clients.claim()
).
如果这是对已注册 service worker scope
下的页面的后续访问,则将设置 navigator.serviceWorker.controller
,并且该页面将在 service worker 的控制下。