gpt4 book ai didi

javascript - 重新加载页面后,Service Worker 显示为已删除

转载 作者:行者123 更新时间:2023-12-04 11:39:15 25 4
gpt4 key购买 nike

我正在使用 react 并尝试实现服务 worker ,我已经为推送通知编写了一个迷你服务 worker 文件,当用户单击按钮时正在注册服务 worker ,现在如果我发送推送消息,它会按预期工作,但是当我重新加载时页面,应用中的service worker虽然注册成功却显示为已删除,请引用截图
服务 worker 代码

    const self = this;

function receivePushNotification(event) {
console.log('[Service Worker] Push Received.');
console.log(event);

// const { image, tag, url, title, text } = event.data.json();

const options = {
data: '/neworders',
body: 'New order request',
vibrate: [200, 100, 200],
badge: 'https://spyna.it/icons/favicon.ico',
actions: [
{
action: 'Detail',
title: 'View',
icon: 'https://via.placeholder.com/128/ff0000',
},
],
};
event.waitUntil(self.registration.showNotification('Hello', options));
}

function openPushNotification(event) {
console.log(
'[Service Worker] Notification click Received.',
event.notification.data
);

event.notification.close();
event.waitUntil(clients.openWindow(event.notification.data));
}

self.addEventListener('install', function (event) {
console.log({ event });
// The promise that skipWaiting() returns can be safely ignored.
self.skipWaiting();

// Perform any other actions required for your
// service worker to install, potentially inside
// of event.waitUntil();
});

self.addEventListener('activate', (event) => {
console.log('V1 now ready to handle fetches!');
});
self.addEventListener('push', receivePushNotification);
self.addEventListener('notificationclick', openPushNotification);
注册服务 worker 函数
function registerServiceWorker() {
return navigator.serviceWorker.register('/newsw.js');
}
Service Worker 已删除截图
Service worker deleted screenshot

最佳答案

我怀疑这是因为您打开了“重新加载时更新”。
这会强制服务 worker 在您重新加载页面时进行更新。 (阅读更多 here )

关于javascript - 重新加载页面后,Service Worker 显示为已删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68199281/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com