From 022c9d1eac5c0e44a12f83e52a34c7f779e56811 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B2=81=E6=A0=91=E4=BA=BA?= Date: Wed, 11 Oct 2023 23:06:09 +0100 Subject: [PATCH] chore: pwa update prompt --- src/main.tsx | 3 ++- src/pwa.ts | 10 ++++++++++ src/vite-env.d.ts | 8 ++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 src/pwa.ts diff --git a/src/main.tsx b/src/main.tsx index bc08353..f5e600c 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -1,3 +1,4 @@ +import './pwa'; import React from 'react'; import ReactDOM from 'react-dom/client'; @@ -13,5 +14,5 @@ SyntaxHighlighter.registerLanguage('bash', hljsSyntaxBash); ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render( - + , ); diff --git a/src/pwa.ts b/src/pwa.ts new file mode 100644 index 0000000..17010c4 --- /dev/null +++ b/src/pwa.ts @@ -0,0 +1,10 @@ +import { registerSW } from 'virtual:pwa-register'; + +const updateSW = registerSW({ + onNeedRefresh() { + if (confirm('应用程序已更新,是否刷新?')) { + updateSW(); + } + }, + onOfflineReady() {}, +}); diff --git a/src/vite-env.d.ts b/src/vite-env.d.ts index 11f02fe..7c041c4 100644 --- a/src/vite-env.d.ts +++ b/src/vite-env.d.ts @@ -1 +1,9 @@ /// + +module 'virtual:pwa-register' { + /** + * See: {@link https://vite-pwa-org.netlify.app/guide/prompt-for-update.html} + */ + // eslint-disable-next-line @typescript-eslint/no-unused-vars + declare function registerSW(opts: unknown): () => void; +}