mirror of
https://github.com/dergigi/boris.git
synced 2025-12-17 06:34:24 +01:00
chore(lint): fix service worker typings and satisfy ESLint for worker globals
This commit is contained in:
17
src/sw.ts
17
src/sw.ts
@@ -1,4 +1,6 @@
|
|||||||
/// <reference lib="webworker" />
|
/// <reference lib="webworker" />
|
||||||
|
/* eslint-env worker */
|
||||||
|
/* global ServiceWorkerGlobalScope, ExtendableMessageEvent, FetchEvent */
|
||||||
import { clientsClaim } from 'workbox-core'
|
import { clientsClaim } from 'workbox-core'
|
||||||
import { precacheAndRoute, cleanupOutdatedCaches } from 'workbox-precaching'
|
import { precacheAndRoute, cleanupOutdatedCaches } from 'workbox-precaching'
|
||||||
import { registerRoute, NavigationRoute } from 'workbox-routing'
|
import { registerRoute, NavigationRoute } from 'workbox-routing'
|
||||||
@@ -6,7 +8,8 @@ import { StaleWhileRevalidate } from 'workbox-strategies'
|
|||||||
import { ExpirationPlugin } from 'workbox-expiration'
|
import { ExpirationPlugin } from 'workbox-expiration'
|
||||||
import { CacheableResponsePlugin } from 'workbox-cacheable-response'
|
import { CacheableResponsePlugin } from 'workbox-cacheable-response'
|
||||||
|
|
||||||
declare let self: ServiceWorkerGlobalScope
|
// Narrow the global service worker scope for proper typings
|
||||||
|
const sw = self as unknown as ServiceWorkerGlobalScope
|
||||||
|
|
||||||
// Precache all build assets (app shell)
|
// Precache all build assets (app shell)
|
||||||
// @ts-ignore - __WB_MANIFEST is injected by vite-plugin-pwa
|
// @ts-ignore - __WB_MANIFEST is injected by vite-plugin-pwa
|
||||||
@@ -16,7 +19,7 @@ precacheAndRoute(self.__WB_MANIFEST)
|
|||||||
cleanupOutdatedCaches()
|
cleanupOutdatedCaches()
|
||||||
|
|
||||||
// Take control immediately
|
// Take control immediately
|
||||||
self.skipWaiting()
|
sw.skipWaiting()
|
||||||
clientsClaim()
|
clientsClaim()
|
||||||
|
|
||||||
console.log('[SW] Boris service worker loaded')
|
console.log('[SW] Boris service worker loaded')
|
||||||
@@ -27,7 +30,7 @@ registerRoute(
|
|||||||
({ request, url }) => {
|
({ request, url }) => {
|
||||||
const isImage = request.destination === 'image' ||
|
const isImage = request.destination === 'image' ||
|
||||||
/\.(jpg|jpeg|png|gif|webp|svg)$/i.test(url.pathname)
|
/\.(jpg|jpeg|png|gif|webp|svg)$/i.test(url.pathname)
|
||||||
return isImage && url.origin !== self.location.origin
|
return isImage && url.origin !== sw.location.origin
|
||||||
},
|
},
|
||||||
new StaleWhileRevalidate({
|
new StaleWhileRevalidate({
|
||||||
cacheName: 'boris-images',
|
cacheName: 'boris-images',
|
||||||
@@ -49,7 +52,7 @@ registerRoute(
|
|||||||
({ request, url }) => {
|
({ request, url }) => {
|
||||||
const accept = request.headers.get('accept') || ''
|
const accept = request.headers.get('accept') || ''
|
||||||
const isHTML = accept.includes('text/html')
|
const isHTML = accept.includes('text/html')
|
||||||
const isCrossOrigin = url.origin !== self.location.origin
|
const isCrossOrigin = url.origin !== sw.location.origin
|
||||||
// Exclude relay connections and local URLs
|
// Exclude relay connections and local URLs
|
||||||
const isNotRelay = !url.protocol.includes('ws')
|
const isNotRelay = !url.protocol.includes('ws')
|
||||||
return isHTML && isCrossOrigin && isNotRelay
|
return isHTML && isCrossOrigin && isNotRelay
|
||||||
@@ -90,14 +93,14 @@ const navigationRoute = new NavigationRoute(
|
|||||||
registerRoute(navigationRoute)
|
registerRoute(navigationRoute)
|
||||||
|
|
||||||
// Listen for messages from the app
|
// Listen for messages from the app
|
||||||
self.addEventListener('message', (event) => {
|
sw.addEventListener('message', (event: ExtendableMessageEvent) => {
|
||||||
if (event.data && event.data.type === 'SKIP_WAITING') {
|
if (event.data && event.data.type === 'SKIP_WAITING') {
|
||||||
self.skipWaiting()
|
sw.skipWaiting()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// Log fetch errors for debugging (doesn't affect functionality)
|
// Log fetch errors for debugging (doesn't affect functionality)
|
||||||
self.addEventListener('fetch', (event) => {
|
sw.addEventListener('fetch', (event: FetchEvent) => {
|
||||||
const url = new URL(event.request.url)
|
const url = new URL(event.request.url)
|
||||||
|
|
||||||
// Don't interfere with WebSocket connections (relay traffic)
|
// Don't interfere with WebSocket connections (relay traffic)
|
||||||
|
|||||||
Reference in New Issue
Block a user