-
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.
- Loading branch information
1 parent
2c99421
commit 39b357e
Showing
8 changed files
with
174 additions
and
3 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,12 @@ | ||
import { | ||
defineConfig, | ||
minimal2023Preset as preset, | ||
} from '@vite-pwa/assets-generator/config' | ||
|
||
export default defineConfig({ | ||
headLinkOptions: { | ||
preset: '2023', | ||
}, | ||
preset, | ||
images: ['public/icon-512.png'], | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { snackbar } from "mdui/functions/snackbar.js"; | ||
import { registerSW } from 'virtual:pwa-register'; | ||
|
||
export function initPWA() { | ||
let refreshSW: (reloadPage?: boolean) => Promise<void> | undefined | ||
|
||
let swActivated = false | ||
// 每小时检查一次更新 | ||
const period = 60 * 60 * 1000 | ||
|
||
window.addEventListener('load', () => { | ||
refreshSW = registerSW({ | ||
immediate: true, | ||
onOfflineReady() { | ||
snackbar({ | ||
message: "问心(不包含试题部分)已准备好在离线环境下运行" | ||
}) | ||
}, | ||
onNeedRefresh() { | ||
snackbar({ | ||
message: "问心有新版本", | ||
action: "立即更新", | ||
onActionClick: () => { | ||
refreshSW?.(true) | ||
true | ||
} | ||
}); | ||
}, | ||
onRegisteredSW(swUrl, r) { | ||
if (period <= 0) return | ||
if (r?.active?.state === 'activated') { | ||
swActivated = true | ||
registerPeriodicSync(period, swUrl, r) | ||
} | ||
else if (r?.installing) { | ||
r.installing.addEventListener('statechange', (e) => { | ||
const sw = e.target as ServiceWorker | ||
swActivated = sw.state === 'activated' | ||
if (swActivated) | ||
registerPeriodicSync(period, swUrl, r) | ||
}) | ||
} | ||
}, | ||
}) | ||
}) | ||
} | ||
|
||
/** | ||
* 定时进行同步检查,可根据需要修改时间间隔 | ||
* @param period 检查时间间隔 | ||
* @param swUrl Service Worker URL | ||
* @param r ServiceWorkerRegistration | ||
* @returns | ||
*/ | ||
function registerPeriodicSync(period: number, swUrl: string, r: ServiceWorkerRegistration) { | ||
if (period <= 0) return | ||
|
||
setInterval(async () => { | ||
if ('onLine' in navigator && !navigator.onLine) | ||
return | ||
|
||
const resp = await fetch(swUrl, { | ||
cache: 'no-store', | ||
headers: { | ||
'cache': 'no-store', | ||
'cache-control': 'no-cache', | ||
}, | ||
}) | ||
|
||
if (resp?.status === 200) | ||
await r.update() | ||
}, period) | ||
} |
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,27 @@ | ||
/// <reference lib="webworker" /> | ||
import { cleanupOutdatedCaches, createHandlerBoundToURL, precacheAndRoute } from 'workbox-precaching' | ||
import { NavigationRoute, registerRoute } from 'workbox-routing' | ||
|
||
declare let self: ServiceWorkerGlobalScope | ||
|
||
self.addEventListener('message', (event) => { | ||
if (event.data && event.data.type === 'SKIP_WAITING') | ||
self.skipWaiting() | ||
}) | ||
|
||
// self.__WB_MANIFEST is the default injection point | ||
precacheAndRoute(self.__WB_MANIFEST) | ||
|
||
// clean old assets | ||
cleanupOutdatedCaches() | ||
|
||
let allowlist: RegExp[] | undefined | ||
// in dev mode, we disable precaching to avoid caching issues | ||
if (import.meta.env.DEV) | ||
allowlist = [/^\/$/] | ||
|
||
// to allow work offline | ||
registerRoute(new NavigationRoute( | ||
createHandlerBoundToURL('index.html'), | ||
{ allowlist }, | ||
)) |
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/client" /> | ||
/// <reference types="vite-plugin-pwa/vanillajs" /> |
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