新增 PWA 更新提示並整理 README
This commit is contained in:
BIN
public/apple-touch-icon.png
Normal file
BIN
public/apple-touch-icon.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 3.6 KiB |
26
public/manifest.webmanifest
Normal file
26
public/manifest.webmanifest
Normal file
@@ -0,0 +1,26 @@
|
||||
{
|
||||
"name": "羽球記分板",
|
||||
"short_name": "羽球記分板",
|
||||
"description": "羽毛球記分板,可在手機主畫面像 App 一樣開啟。",
|
||||
"start_url": "/",
|
||||
"scope": "/",
|
||||
"display": "standalone",
|
||||
"background_color": "#f4ecd8",
|
||||
"theme_color": "#143f49",
|
||||
"lang": "zh-Hant",
|
||||
"orientation": "portrait",
|
||||
"icons": [
|
||||
{
|
||||
"src": "/pwa-192.png",
|
||||
"sizes": "192x192",
|
||||
"type": "image/png",
|
||||
"purpose": "any maskable"
|
||||
},
|
||||
{
|
||||
"src": "/pwa-512.png",
|
||||
"sizes": "512x512",
|
||||
"type": "image/png",
|
||||
"purpose": "any maskable"
|
||||
}
|
||||
]
|
||||
}
|
||||
BIN
public/pwa-192.png
Normal file
BIN
public/pwa-192.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 4.2 KiB |
BIN
public/pwa-512.png
Normal file
BIN
public/pwa-512.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 12 KiB |
87
public/sw.js
Normal file
87
public/sw.js
Normal file
@@ -0,0 +1,87 @@
|
||||
const CACHE_NAME = 'badminton-scoreboard-v1'
|
||||
const APP_SHELL = [
|
||||
'/',
|
||||
'/index.html',
|
||||
'/manifest.webmanifest',
|
||||
'/favicon.svg',
|
||||
'/apple-touch-icon.png',
|
||||
'/pwa-192.png',
|
||||
'/pwa-512.png',
|
||||
]
|
||||
|
||||
self.addEventListener('install', (event) => {
|
||||
event.waitUntil(
|
||||
caches.open(CACHE_NAME).then((cache) => cache.addAll(APP_SHELL)),
|
||||
)
|
||||
self.skipWaiting()
|
||||
})
|
||||
|
||||
self.addEventListener('activate', (event) => {
|
||||
event.waitUntil(
|
||||
caches.keys().then((keys) =>
|
||||
Promise.all(
|
||||
keys.map((key) => {
|
||||
if (key !== CACHE_NAME) {
|
||||
return caches.delete(key)
|
||||
}
|
||||
|
||||
return Promise.resolve(false)
|
||||
}),
|
||||
),
|
||||
),
|
||||
)
|
||||
self.clients.claim()
|
||||
})
|
||||
|
||||
self.addEventListener('message', (event) => {
|
||||
if (event.data?.type === 'SKIP_WAITING') {
|
||||
self.skipWaiting()
|
||||
}
|
||||
})
|
||||
|
||||
self.addEventListener('fetch', (event) => {
|
||||
if (event.request.method !== 'GET') {
|
||||
return
|
||||
}
|
||||
|
||||
const requestUrl = new URL(event.request.url)
|
||||
|
||||
if (requestUrl.origin !== self.location.origin) {
|
||||
return
|
||||
}
|
||||
|
||||
event.respondWith(
|
||||
caches.match(event.request).then(async (cachedResponse) => {
|
||||
if (cachedResponse) {
|
||||
return cachedResponse
|
||||
}
|
||||
|
||||
try {
|
||||
const networkResponse = await fetch(event.request)
|
||||
|
||||
if (
|
||||
networkResponse.ok &&
|
||||
(event.request.destination === 'document' ||
|
||||
event.request.destination === 'script' ||
|
||||
event.request.destination === 'style' ||
|
||||
event.request.destination === 'image' ||
|
||||
requestUrl.pathname.startsWith('/assets/'))
|
||||
) {
|
||||
const cache = await caches.open(CACHE_NAME)
|
||||
cache.put(event.request, networkResponse.clone())
|
||||
}
|
||||
|
||||
return networkResponse
|
||||
} catch (error) {
|
||||
if (event.request.mode === 'navigate') {
|
||||
const fallback = await caches.match('/index.html')
|
||||
if (fallback) {
|
||||
return fallback
|
||||
}
|
||||
}
|
||||
|
||||
throw error
|
||||
}
|
||||
}),
|
||||
)
|
||||
})
|
||||
Reference in New Issue
Block a user