【发布时间】:2017-12-19 07:13:34
【问题描述】:
我对@987654321@ 和reactjs 有疑问。
建议是通知用户有关缓存内容或何时有新内容可用,以便用户了解缓存内容。
我现在的问题是,我该如何通知用户?
当我使用create-react-app 时,registerServiceWorker.js 中有这段代码,上面写着:
此时,旧内容将被清除,并且 新内容将被添加到缓存中。这是完美的 是时候显示“新内容可用;请刷新”。 在您的网络应用中发送消息。
function registerValidSW(swUrl) {
navigator.serviceWorker
.register(swUrl)
.then(registration => {
registration.onupdatefound = () => {
const installingWorker = registration.installing;
installingWorker.onstatechange = () => {
if (installingWorker.state === 'installed') {
if (navigator.serviceWorker.controller) {
// At this point, the old content will have been purged and
// the fresh content will have been added to the cache.
// It's the perfect time to display a "New content is
// available; please refresh." message in your web app.
console.log('New content is available; please refresh.');
} 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.');
}
}
};
};
})
.catch(error => {
console.error('Error during service worker registration:', error);
});
}
但实际上在这个脚本上,当然,我无权访问document,因为服务工作者远离脚本工作。
如何在反应组件中处理这个问题?
其他人如何处理此问题并通知用户?
【问题讨论】:
标签: reactjs caching service-worker create-react-app