diff options
author | crupest <crupest@outlook.com> | 2020-07-13 20:59:52 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2020-07-13 20:59:52 +0800 |
commit | e69190abb09661caa19fa3905a0d8f3b7e72648b (patch) | |
tree | 8030e6a2539347ccb12f0a6bbd31a1f390c6a7b7 /Timeline/ClientApp/src/app/service-worker.tsx | |
parent | 0a7c884be668267003d7666b444f1022c99a7148 (diff) | |
download | timeline-e69190abb09661caa19fa3905a0d8f3b7e72648b.tar.gz timeline-e69190abb09661caa19fa3905a0d8f3b7e72648b.tar.bz2 timeline-e69190abb09661caa19fa3905a0d8f3b7e72648b.zip |
Move front end to a submodule.
Diffstat (limited to 'Timeline/ClientApp/src/app/service-worker.tsx')
-rw-r--r-- | Timeline/ClientApp/src/app/service-worker.tsx | 109 |
1 files changed, 0 insertions, 109 deletions
diff --git a/Timeline/ClientApp/src/app/service-worker.tsx b/Timeline/ClientApp/src/app/service-worker.tsx deleted file mode 100644 index 0dbab480..00000000 --- a/Timeline/ClientApp/src/app/service-worker.tsx +++ /dev/null @@ -1,109 +0,0 @@ -import React from 'react'; -import { Button } from 'reactstrap'; -import { useTranslation } from 'react-i18next'; - -import { pushAlert } from './common/alert-service'; - -if ('serviceWorker' in navigator) { - let isThisTriggerUpgrade = false; - - const upgradeSuccessLocalStorageKey = 'TIMELINE_UPGRADE_SUCCESS'; - - if (window.localStorage.getItem(upgradeSuccessLocalStorageKey)) { - pushAlert({ - message: { - type: 'i18n', - key: 'serviceWorker.upgradeSuccess', - }, - type: 'success', - }); - window.localStorage.removeItem(upgradeSuccessLocalStorageKey); - } - - void import('workbox-window').then(({ Workbox, messageSW }) => { - const wb = new Workbox('/sw.js'); - let registration: ServiceWorkerRegistration | undefined; - - // externalactivated is not usable but I still use its name. - wb.addEventListener('controlling', () => { - const upgradeReload = (): void => { - window.localStorage.setItem(upgradeSuccessLocalStorageKey, 'true'); - window.location.reload(); - }; - - if (isThisTriggerUpgrade) { - upgradeReload(); - } else { - const Message: React.FC = () => { - const { t } = useTranslation(); - return ( - <> - {t('serviceWorker.externalActivatedPrompt')} - <Button color="success" size="sm" onClick={upgradeReload} outline> - {t('serviceWorker.reloadNow')} - </Button> - </> - ); - }; - - pushAlert({ - message: Message, - dismissTime: 'never', - type: 'warning', - }); - } - }); - - wb.addEventListener('activated', (event) => { - if (!event.isUpdate) { - pushAlert({ - message: { - type: 'i18n', - key: 'serviceWorker.availableOffline', - }, - type: 'success', - }); - } - }); - - const showSkipWaitingPrompt = (): void => { - const upgrade = (): void => { - isThisTriggerUpgrade = true; - if (registration && registration.waiting) { - // Send a message to the waiting service worker, - // instructing it to activate. - // Note: for this to work, you have to add a message - // listener in your service worker. See below. - void messageSW(registration.waiting, { type: 'SKIP_WAITING' }); - } - }; - - const UpgradeMessage: React.FC = () => { - const { t } = useTranslation(); - return ( - <> - {t('serviceWorker.upgradePrompt')} - <Button color="success" size="sm" onClick={upgrade} outline> - {t('serviceWorker.upgradeNow')} - </Button> - </> - ); - }; - - pushAlert({ - message: UpgradeMessage, - dismissTime: 'never', - type: 'success', - }); - }; - - // Add an event listener to detect when the registered - // service worker has installed but is waiting to activate. - wb.addEventListener('waiting', showSkipWaitingPrompt); - wb.addEventListener('externalwaiting', showSkipWaitingPrompt); - - void wb.register().then((reg) => { - registration = reg; - }); - }); -} |