強化房間清理與比賽中分頁限制
This commit is contained in:
49
public/sw.js
49
public/sw.js
@@ -1,7 +1,5 @@
|
||||
const CACHE_NAME = 'badminton-scoreboard-v1'
|
||||
const CACHE_NAME = 'badminton-scoreboard-v2'
|
||||
const APP_SHELL = [
|
||||
'/',
|
||||
'/index.html',
|
||||
'/manifest.webmanifest',
|
||||
'/favicon.png',
|
||||
'/icon.png',
|
||||
@@ -51,19 +49,45 @@ self.addEventListener('fetch', (event) => {
|
||||
return
|
||||
}
|
||||
|
||||
if (requestUrl.pathname.startsWith('/api/')) {
|
||||
event.respondWith(fetch(event.request))
|
||||
return
|
||||
}
|
||||
|
||||
const isNavigationRequest =
|
||||
event.request.mode === 'navigate' || event.request.destination === 'document'
|
||||
|
||||
if (isNavigationRequest) {
|
||||
event.respondWith(
|
||||
fetch(event.request)
|
||||
.then(async (networkResponse) => {
|
||||
if (networkResponse.ok) {
|
||||
const cache = await caches.open(CACHE_NAME)
|
||||
cache.put('/index.html', networkResponse.clone())
|
||||
}
|
||||
|
||||
return networkResponse
|
||||
})
|
||||
.catch(async () => {
|
||||
const fallback = await caches.match('/index.html')
|
||||
if (fallback) {
|
||||
return fallback
|
||||
}
|
||||
|
||||
throw new Error('Navigation request failed')
|
||||
}),
|
||||
)
|
||||
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 === 'script' ||
|
||||
event.request.destination === 'style' ||
|
||||
event.request.destination === 'image' ||
|
||||
requestUrl.pathname.startsWith('/assets/'))
|
||||
@@ -74,11 +98,8 @@ self.addEventListener('fetch', (event) => {
|
||||
|
||||
return networkResponse
|
||||
} catch (error) {
|
||||
if (event.request.mode === 'navigate') {
|
||||
const fallback = await caches.match('/index.html')
|
||||
if (fallback) {
|
||||
return fallback
|
||||
}
|
||||
if (cachedResponse) {
|
||||
return cachedResponse
|
||||
}
|
||||
|
||||
throw error
|
||||
|
||||
Reference in New Issue
Block a user