Files
cozsweet-frontend-nextjs/src/app/sw.ts
T

55 lines
1.7 KiB
TypeScript
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.
/// <reference lib="esnext" />
/// <reference lib="webworker" />
/**
* Cozsweet PWA Service Worker (Serwist source)
*
* 编译链:
* src/app/sw.ts (此文件)
* → esbuild (Serwist Turbopack 在 build 时跑)
* → /serwist/sw.js (App Router Route Handler 服务)
*
* precache 列表由 `withSerwist` 在 build 时注入 `self.__SW_MANIFEST`。
* Runtime cache 用 `defaultCache`Serwist 推荐的 Google Fonts / Static / Image
* 缓存策略)—— 对聊天类应用够用。
*
* 配置:
* - skipWaiting: true 新 SW 立即 activate,避免旧 SW 卡住页面
* - clientsClaim: true 新 SW 立即接管所有打开的 client
* - navigationPreload: 用 preload API 加速文档请求(资源请求 SW 完成前先发)
*
* Offline fallback`/~offline` 路由(serwist route handler 里也登记了
* `additionalPrecacheEntries`,双保险)。对应页面:
* `src/app/~offline/page.tsx`。
*/
import { defaultCache } from "@serwist/turbopack/worker";
import type { PrecacheEntry, SerwistGlobalConfig } from "serwist";
import { Serwist } from "serwist";
declare global {
interface WorkerGlobalScope extends SerwistGlobalConfig {
__SW_MANIFEST: (PrecacheEntry | string)[] | undefined;
}
}
declare const self: ServiceWorkerGlobalScope;
const serwist = new Serwist({
precacheEntries: self.__SW_MANIFEST,
skipWaiting: true,
clientsClaim: true,
navigationPreload: true,
runtimeCaching: defaultCache,
fallbacks: {
entries: [
{
url: "/~offline",
matcher({ request }) {
return request.destination === "document";
},
},
],
},
});
serwist.addEventListeners();