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; +}