From abda382b9626b092c485979d3fa98e29981e721d Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Mon, 7 Jul 2025 14:35:25 +0000 Subject: [PATCH] Refactor service worker types and extract type definitions Co-authored-by: jxxghp --- src/service-worker.ts | 24 ++----------------- src/types/service-worker-sync.d.ts | 38 ++++++++++++++++++++++++++++++ src/types/workbox-precaching.d.ts | 23 ++++++++++++++++++ 3 files changed, 63 insertions(+), 22 deletions(-) create mode 100644 src/types/service-worker-sync.d.ts create mode 100644 src/types/workbox-precaching.d.ts diff --git a/src/service-worker.ts b/src/service-worker.ts index 562351b1..4ab29004 100644 --- a/src/service-worker.ts +++ b/src/service-worker.ts @@ -1,28 +1,8 @@ import { cleanupOutdatedCaches, precacheAndRoute } from 'workbox-precaching' // Service Worker 类型声明 -declare let self: ServiceWorkerGlobalScope - -// 扩展ServiceWorkerRegistration类型以支持sync -interface SyncManager { - register(tag: string): Promise -} - -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 }> } // 缓存版本控制 diff --git a/src/types/service-worker-sync.d.ts b/src/types/service-worker-sync.d.ts new file mode 100644 index 00000000..efa74235 --- /dev/null +++ b/src/types/service-worker-sync.d.ts @@ -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 + } + + /** + * 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 + } +} \ No newline at end of file diff --git a/src/types/workbox-precaching.d.ts b/src/types/workbox-precaching.d.ts new file mode 100644 index 00000000..19268a1e --- /dev/null +++ b/src/types/workbox-precaching.d.ts @@ -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 +} \ No newline at end of file