Files
wrong-notebook/public/sw.js
margrop 07ba47ccd4 feat: 完善 PWA,补齐多尺寸图标并加入 Service Worker 与离线页
- manifest 增加 192/512 与 maskable 图标、id、scope,屏幕方向改为 any 以支持平板横屏
- 新增 public/sw.js:静态资源缓存优先,页面导航断网时显示离线页,API 不缓存
- iOS 主屏幕图标改用 180px 的 apple-touch-icon
- 中间件放行 sw.js 与 offline.html

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 08:50:18 +08:00

85 lines
2.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* 错题本 PWA Service Worker
*
* - 页面导航:始终走网络,断网时返回离线页 /offline.html
* - /_next/static、/icons:文件名带哈希或基本不变,缓存优先
* - API 与其他请求不经过缓存,保证数据始终来自服务器
*/
const VERSION = 'v1';
const STATIC_CACHE = `wrong-notebook-static-${VERSION}`;
const OFFLINE_URL = '/offline.html';
const PRECACHE = [OFFLINE_URL, '/icons/icon-192.png', '/icons/icon-512.png'];
/** 静态缓存最多保留的条目数,超出后删除最早写入的 */
const MAX_STATIC_ENTRIES = 300;
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(STATIC_CACHE)
.then((cache) => cache.addAll(PRECACHE))
.then(() => self.skipWaiting())
);
});
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys()
.then((keys) => Promise.all(
keys
.filter((key) => key.startsWith('wrong-notebook-') && key !== STATIC_CACHE)
.map((key) => caches.delete(key))
))
.then(() => self.clients.claim())
);
});
function isStaticAsset(url) {
return url.pathname.startsWith('/_next/static/') || url.pathname.startsWith('/icons/');
}
async function trimCache(cache) {
const keys = await cache.keys();
const excess = keys.length - MAX_STATIC_ENTRIES;
for (let i = 0; i < excess; i++) {
if (!keys[i].url.endsWith(OFFLINE_URL)) await cache.delete(keys[i]);
}
}
async function cacheFirst(request) {
const cached = await caches.match(request);
if (cached) return cached;
const response = await fetch(request);
if (response.ok && response.type === 'basic') {
const cache = await caches.open(STATIC_CACHE);
await cache.put(request, response.clone());
trimCache(cache);
}
return response;
}
async function networkWithOfflineFallback(request) {
try {
return await fetch(request);
} catch (error) {
const offline = await caches.match(OFFLINE_URL);
if (offline) return offline;
throw error;
}
}
self.addEventListener('fetch', (event) => {
const { request } = event;
if (request.method !== 'GET') return;
const url = new URL(request.url);
if (url.origin !== self.location.origin) return;
if (request.mode === 'navigate') {
event.respondWith(networkWithOfflineFallback(request));
return;
}
if (isStaticAsset(url)) {
event.respondWith(cacheFirst(request));
}
});