更新PWA React App(使用CRA)使用用户单击按钮通过服务工作者
我想在可用新内容时更新PWA应用程序。既然我们无法从服务工作者访问DOM,我该怎么做? 服务工作者正确识别新内容,但是我无法绑定钩子或上下文以显示React中的模式或更新按钮。有帮助吗?
服务工作者寄存器和 onupdate 函数:
function RegisterValidSW(swUrl, config) {
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.
// setUpdate(true);
// registration.waiting.postMessage({ type: 'SKIP_WAITING' });
console.log(
'New content is available and will be used when all ' +
'tabs for this page are closed. See https://cra.link/PWA.'
);
// 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);
});
}
此功能在 serviceworkerRegistration.js 文件中。 我们可以添加注册。我该如何解决?
I want to update PWA application when new content is available. Since we can't access DOM from service worker, how can I exactly do that?
The service worker correctly recognize the new content, but I can't bind a hook or context to show my modal or update button in react. Any help?
service worker register and onUpdate function:
function RegisterValidSW(swUrl, config) {
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.
// setUpdate(true);
// registration.waiting.postMessage({ type: 'SKIP_WAITING' });
console.log(
'New content is available and will be used when all ' +
'tabs for this page are closed. See https://cra.link/PWA.'
);
// 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);
});
}
this function is in the serviceWorkerRegistration.js file.
We can add registration.waiting.postMessage({ type: 'SKIP_WAITING' }); function to call skipWaiting but it doesn't wait for user interaction and can't reload the page. how can I solve that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
注册服务工作者时,您可以尝试通过Update功能传递。在该功能中,遵循页面重新加载后,向服务工作者发送Skip_waiting消息。
您的服务器工人应具有SKIP_WAITING消息厂商。这样的事情:
这对我有用。
我仍在努力的是,如果用户打开应用程序,则如何更新应用程序。与此一样,
永远打开它...
,并且只会 对您的用例。
You can try passing in an onUpdate function when you register the service worker. In that function send a SKIP_WAITING message to the service worker followed a page reload.
Your server worker should have SKIP_WAITING message hander. Something like this:
This is working for me.
What I am still struggling with is how to get the app updated if the user leaves the app open. As in,
and just leaves it open forever and ever...
This should probably go in a new question, but I thought it might be relevant to your use case.
基本上,例如,您有一个PWA组件。
在此组件中,您可以创建一个对话模式,以显示用户新更新。
然后,您在
componentDidmount
或useffect(()=> {},[],[])
中注册此组件内部的PWA。但是,这样,您将更新功能传递到这样的登记册中。
// ============== serviceworkerRegisteration.js
// ====================== pwa/index.js
// ================== pwa/dialog.js
registerpwaopeninghandler
添加到service> serviceworkerRegisteration
for for for for for Of For兼容性,例如用户取消更新模式并在他再次卷回我们的选项卡后切换到另一个选项卡时,我们再次显示对话框。basically, you have a PWA component for example.
in this component, you create a dialog modal to show the user new updates.
then you register the PWA inside of this component in
componentDidMount
oruseEffect(() => {} , [])
.but in this way you pass an update function into register like this.
// ============= ServiceWorkerRegisteration.js
// ============= PWA/index.js
// ============= PWA/dialog.js
registerPwaOpeningHandler
function added toServiceWorkerRegisteration
for more compatibility like when user cancel the update modal and switch to another tab after he comeback to our tab again we show the dialog once more.