-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add service worker auto update prompt
- Loading branch information
1 parent
b431754
commit c346c06
Showing
7 changed files
with
97 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { useRegisterSW } from 'virtual:pwa-register/react' | ||
import {version} from "../conf.ts"; | ||
import {useTranslation} from "react-i18next"; | ||
import {useToast} from "./ui/use-toast.ts"; | ||
import {useEffect} from "react"; | ||
import {ToastAction} from "./ui/toast.tsx"; | ||
|
||
function ReloadPrompt() { | ||
const { t } = useTranslation(); | ||
const { toast } = useToast(); | ||
|
||
const { | ||
offlineReady: [offlineReady, setOfflineReady], | ||
needRefresh: [needRefresh, setNeedRefresh], | ||
updateServiceWorker, | ||
} = useRegisterSW({ | ||
onRegisteredSW() { | ||
console.debug(`[service] service worker registered (version ${version})`); | ||
}, | ||
onRegisterError(error) { | ||
console.log(`[service] service worker registration failed: ${error.message}`); | ||
}, | ||
}); | ||
|
||
useEffect(() => { | ||
if (offlineReady) { | ||
toast({ | ||
title: t('service.offline-title'), | ||
description: t('service.offline'), | ||
}) | ||
} | ||
|
||
if (needRefresh) { | ||
toast({ | ||
title: t('service.title'), | ||
description: t('service.description'), | ||
action: ( | ||
<ToastAction altText={t('service.update')} onClick={() => updateServiceWorker(true)}> | ||
{t('service.update')} | ||
</ToastAction> | ||
), | ||
}); | ||
|
||
setOfflineReady(false); | ||
setNeedRefresh(false); | ||
} | ||
}, []); | ||
|
||
return <></>; | ||
} | ||
|
||
export default ReloadPrompt; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
declare module 'virtual:pwa-register/react' { | ||
// eslint-disable-next-line @typescript-eslint/prefer-ts-expect-error | ||
// @ts-expect-error ignore when React is not installed | ||
import type { Dispatch, SetStateAction } from 'react' | ||
import type { RegisterSWOptions } from 'vite-plugin-pwa/types' | ||
|
||
export type { RegisterSWOptions } | ||
|
||
export function useRegisterSW(options?: RegisterSWOptions): { | ||
needRefresh: [boolean, Dispatch<SetStateAction<boolean>>] | ||
offlineReady: [boolean, Dispatch<SetStateAction<boolean>>] | ||
updateServiceWorker: (reloadPage?: boolean) => Promise<void> | ||
onRegistered: (registration: ServiceWorkerRegistration) => void | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters