答案:通过Service Worker结合Cache API实现离线访问,首先注册并安装Service Worker,在install阶段预缓存核心资源;然后根据资源类型选择网络优先或缓存优先策略,如HTML采用网络优先降级离线页,图片采用缓存优先;同时在activate阶段清除旧缓存,并动态缓存运行时请求,确保核心功能可用且内容及时更新。

要实现可靠的离线体验,关键在于利用 Service Worker 拦截网络请求,并通过 Cache API 缓存关键资源。这样即使用户断网,页面仍能正常加载和使用。
注册并安装 Service Worker
在页面加载时注册 Service Worker,浏览器会在后台安装它。安装阶段适合预缓存核心资源,比如 HTML、CSS、JS 和离线页面。
示例代码:
if ('serviceWorker' in navigator) { navigator.serviceWorker.register('/sw.js') .then(reg => console.log('SW registered:', reg)) .catch(err => console.log('SW registration failed:', err)); }
const CACHE_NAME = 'v1'; const PRECACHE_URLS = ['/', '/styles.css', '/app.js', '/offline.html']; self.addEventListener('install', event => { event.waitUntil( caches.open(CACHE_NAME) .then(cache => cache.addAll(PRECACHE_URLS)) ); });
缓存策略:网络优先或缓存优先
根据资源类型选择合适的缓存策略。静态资源可用缓存优先,API 请求建议网络优先并降级到缓存。
例如,对于 HTML 页面使用网络优先,失败时返回预缓存的离线页:
self.addEventListener('fetch', event => { const { request } = event; if (request.destination === 'document') { event.respondWith( fetch(request) .then(res => { const copy = res.clone(); caches.open(CACHE_NAME).then(cache => cache.put(request, copy)); return res; }) .catch(() => caches.match('/offline.html')) ); } });
图片等静态资源可采用缓存优先:
if (request.destination === 'image') { event.respondWith( caches.match(request) .then(cached => cached || fetch(request)) ); }
动态缓存与版本管理
除了预缓存,运行时请求也应被记录,提升后续访问体验。同时注意缓存版本控制,避免旧内容残留。
在 activate 阶段清理旧缓存:
self.addEventListener('activate', event => { event.waitUntil( caches.keys().then(keys => Promise.all( keys.filter(key => key !== CACHE_NAME) .map(key => caches.delete(key)) ) ) ); });
动态缓存响应:
caches.open(CACHE_NAME) .then(cache => cache.put(request, response));
基本上就这些。合理组合预缓存、运行时缓存和清晰的缓存更新逻辑,就能构建出稳定可靠的离线应用体验。关键是确保核心路径可用,且缓存能及时更新。


