Skip to content

Commit

Permalink
remove requestIdleCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
mebtte committed Sep 8, 2023
1 parent c48eaa3 commit dd00179
Showing 1 changed file with 53 additions and 57 deletions.
110 changes: 53 additions & 57 deletions apps/pwa/src/updater.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<hi@mebtte.com>
*/
if (process.env.NODE_ENV === 'production') {
let updateNoticeId: string = '';
wb.addEventListener('waiting', () => {
updateNoticeId = notice.info(
<VersionUpdater>
<div className="text">{t('pwa_update_question')}</div>
<div className="action-box">
<IconButton
className="action"
onClick={() => {
wb.messageSkipWaiting();
return Promise.race([
new Promise((resolve) =>
wb.addEventListener('controlling', resolve),
),
sleep(5000),
]).then(() => window.location.reload());
}}
>
<MdCheck />
</IconButton>
<IconButton
className="action"
onClick={() => notice.close(updateNoticeId)}
>
<MdClose />
</IconButton>
</div>
</VersionUpdater>,
{ duration: 0, closable: false },
);
});

/**
* 生产模式下询问是否升级
* 开发模式下默认升级
* 定时检查更新
* @author mebtte<hi@mebtte.com>
*/
if (process.env.NODE_ENV === 'production') {
let updateNoticeId: string = '';
wb.addEventListener('waiting', () => {
updateNoticeId = notice.info(
<VersionUpdater>
<div className="text">{t('pwa_update_question')}</div>
<div className="action-box">
<IconButton
className="action"
onClick={() => {
wb.messageSkipWaiting();
return Promise.race([
new Promise((resolve) =>
wb.addEventListener('controlling', resolve),
),
sleep(5000),
]).then(() => window.location.reload());
}}
>
<MdCheck />
</IconButton>
<IconButton
className="action"
onClick={() => notice.close(updateNoticeId)}
>
<MdClose />
</IconButton>
</div>
</VersionUpdater>,
{ duration: 0, closable: false },
);
});

/**
* 定时检查更新
* @author mebtte<hi@mebtte.com>
*/
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()),
);
}
}

0 comments on commit dd00179

Please sign in to comment.