From dd001792b14d63958691d4d7208ae58d5034192d Mon Sep 17 00:00:00 2001 From: mebtte Date: Fri, 8 Sep 2023 16:02:19 +0800 Subject: [PATCH] remove requestIdleCallback --- apps/pwa/src/updater.tsx | 110 +++++++++++++++++++-------------------- 1 file changed, 53 insertions(+), 57 deletions(-) diff --git a/apps/pwa/src/updater.tsx b/apps/pwa/src/updater.tsx index 92e73c85..37f13db5 100644 --- a/apps/pwa/src/updater.tsx +++ b/apps/pwa/src/updater.tsx @@ -28,67 +28,63 @@ const VersionUpdater = styled.div` `; if ('serviceWorker' in navigator) { if (definition.WITH_SW) { - window.requestIdleCallback(() => - import('workbox-window').then(({ Workbox }) => { - const wb = new Workbox('/service_worker.js'); - wb.register(); + import('workbox-window').then(({ Workbox }) => { + const wb = new Workbox('/service_worker.js'); + wb.register(); + + /** + * 生产模式下询问是否升级 + * 开发模式下默认升级 + * @author mebtte + */ + if (process.env.NODE_ENV === 'production') { + let updateNoticeId: string = ''; + wb.addEventListener('waiting', () => { + updateNoticeId = notice.info( + +
{t('pwa_update_question')}
+
+ { + wb.messageSkipWaiting(); + return Promise.race([ + new Promise((resolve) => + wb.addEventListener('controlling', resolve), + ), + sleep(5000), + ]).then(() => window.location.reload()); + }} + > + + + notice.close(updateNoticeId)} + > + + +
+
, + { duration: 0, closable: false }, + ); + }); /** - * 生产模式下询问是否升级 - * 开发模式下默认升级 + * 定时检查更新 * @author mebtte */ - if (process.env.NODE_ENV === 'production') { - let updateNoticeId: string = ''; - wb.addEventListener('waiting', () => { - updateNoticeId = notice.info( - -
{t('pwa_update_question')}
-
- { - wb.messageSkipWaiting(); - return Promise.race([ - new Promise((resolve) => - wb.addEventListener('controlling', resolve), - ), - sleep(5000), - ]).then(() => window.location.reload()); - }} - > - - - notice.close(updateNoticeId)} - > - - -
-
, - { duration: 0, closable: false }, - ); - }); - - /** - * 定时检查更新 - * @author mebtte - */ - window.setInterval(() => { - notice.close(updateNoticeId); - return wb.update(); - }, 1000 * 60 * 60); - } - }), - ); - } else { - window.requestIdleCallback(() => { - window.navigator.serviceWorker - .getRegistrations() - .then((registrations) => - registrations.forEach((registration) => registration.unregister()), - ); + window.setInterval(() => { + notice.close(updateNoticeId); + return wb.update(); + }, 1000 * 60 * 60); + } }); + } else { + window.navigator.serviceWorker + .getRegistrations() + .then((registrations) => + registrations.forEach((registration) => registration.unregister()), + ); } }