Refactor service worker types and extract type definitions

Co-authored-by: jxxghp <jxxghp@163.com>
This commit is contained in:
Cursor Agent
2025-07-07 14:35:25 +00:00
parent c5ab0a2cc6
commit abda382b96
3 changed files with 63 additions and 22 deletions

View File

@@ -1,28 +1,8 @@
import { cleanupOutdatedCaches, precacheAndRoute } from 'workbox-precaching'
// Service Worker 类型声明
declare let self: ServiceWorkerGlobalScope
// 扩展ServiceWorkerRegistration类型以支持sync
interface SyncManager {
register(tag: string): Promise<void>
}
interface ServiceWorkerRegistration {
readonly sync: SyncManager
}
// 扩展ExtendableEvent以支持sync事件
interface SyncEvent extends ExtendableEvent {
readonly tag: string
readonly lastChance: boolean
}
// 扩展ServiceWorkerGlobalScope事件映射
declare global {
interface ServiceWorkerGlobalScopeEventMap {
'sync': SyncEvent
}
declare let self: ServiceWorkerGlobalScope & {
__WB_MANIFEST: Array<{ url: string; revision?: string }>
}
// 缓存版本控制

38
src/types/service-worker-sync.d.ts vendored Normal file
View File

@@ -0,0 +1,38 @@
export {}
declare global {
/**
* Background SyncManager interface as per the Web Background Sync API.
*/
interface SyncManager {
/**
* Registers a one-off sync event with the provided tag.
*/
register(tag: string): Promise<void>
}
/**
* Extension of ServiceWorkerRegistration to include the SyncManager.
*/
interface ServiceWorkerRegistration {
/**
* The SyncManager for background sync operations.
*/
readonly sync: SyncManager
}
/**
* The event fired when a background sync is triggered.
*/
interface SyncEvent extends ExtendableEvent {
readonly tag: string
readonly lastChance: boolean
}
/**
* Extend ServiceWorkerGlobalScope event map to include the sync event type.
*/
interface ServiceWorkerGlobalScopeEventMap {
'sync': SyncEvent
}
}

23
src/types/workbox-precaching.d.ts vendored Normal file
View File

@@ -0,0 +1,23 @@
// Type definitions for workbox-precaching runtime use in service worker
// Simplified subset needed by this project
declare module 'workbox-precaching' {
/**
* A manifest entry generated by Workbox build tools.
*/
export interface ManifestEntry {
url: string
revision?: string
}
/**
* Removes outdated precaches created by older versions of Workbox.
*/
export function cleanupOutdatedCaches(): void
/**
* Adds the supplied manifest entries to the precache list and sets up the
* appropriate route so that they are served from the cache.
*/
export function precacheAndRoute(entries: ManifestEntry[]): void
}