forked from cogentapps/chat-with-gpt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement service worker using vite-plugin-pwa
- Loading branch information
1 parent
f9122c5
commit 36c1cb4
Showing
11 changed files
with
132 additions
and
295 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
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 |
---|---|---|
@@ -1,32 +1,37 @@ | ||
import { Button, Notification } from "@mantine/core"; | ||
import { useCallback } from "react"; | ||
import { useAppDispatch, useAppSelector } from "../store"; | ||
import { resetUpdate, selectUpdateAvailable } from "../store/pwa"; | ||
import { useRegisterSW } from "virtual:pwa-register/react"; | ||
|
||
export function InstallUpdateNotification() { | ||
const updateAvailable = useAppSelector(selectUpdateAvailable); | ||
const dispatch = useAppDispatch(); | ||
const { | ||
offlineReady: [_, setOfflineReady], | ||
needRefresh: [needRefresh, setNeedRefresh], | ||
updateServiceWorker, | ||
} = useRegisterSW({ | ||
onRegistered(r) { | ||
console.log("SW Registered:", r); | ||
}, | ||
onRegisterError(error) { | ||
console.log("SW registration error", error); | ||
}, | ||
}); | ||
|
||
const onClose = useCallback(() => dispatch(resetUpdate()), [dispatch]); | ||
const onClose = () => { | ||
setOfflineReady(false); | ||
setNeedRefresh(false); | ||
}; | ||
|
||
const onUpdate = useCallback(async () => { | ||
dispatch(resetUpdate()); | ||
const onUpdate = useCallback(async () => { | ||
updateServiceWorker(true); | ||
}, []); | ||
|
||
const registration = await navigator.serviceWorker.getRegistration(); | ||
if (registration && registration.waiting) { | ||
registration.waiting.postMessage({ type: "SKIP_WAITING" }); | ||
} | ||
|
||
window.location.reload(); | ||
}, [dispatch]); | ||
|
||
return updateAvailable ? ( | ||
<Notification title="Update available!" onClose={onClose}> | ||
Click{" "} | ||
<Button compact onClick={onUpdate}> | ||
Update now | ||
</Button>{" "} | ||
to get the latest version. | ||
</Notification> | ||
) : null; | ||
return needRefresh ? ( | ||
<Notification title="Update available!" onClose={onClose}> | ||
Click{" "} | ||
<Button compact onClick={onUpdate}> | ||
Update now | ||
</Button>{" "} | ||
to get the latest version. | ||
</Notification> | ||
) : null; | ||
} |
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 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
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
/// <reference types="vite-plugin-comlink/client" /> | ||
/// <reference types="vite-plugin-pwa/client" /> |
Oops, something went wrong.