sw.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Service Worker for 番茄钟应用 PWA 支持
  2. const CACHE_NAME = "time-of-studying-v1";
  3. const STATIC_CACHE_URLS = [
  4. "/",
  5. "/index.html",
  6. // 添加应用可能需要的其他静态资源
  7. "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'%3E%3Ctext y='.9em' font-size='90'%3E⌚%3C/text%3E%3C/svg%3E",
  8. "data:audio/wav;base64,UklGRnoGAABXQVZFZm10IBAAAAABAAEAQB8AAEAfAAABAAgAZGF0YQoGAACBhYqFbF1fdJivrJBhNjVgodDbq2EcBj+a2/LDciUFLIHO8tiJNwgZaLvt559NEAxQp+PwtmMcBjiR1/LMeSwFJHfH8N2QQAoUXrTp66hVFApGn+DyvmwhBSuBzvLZiTYIG2m98OScTgwOUarm7blmFgU7k9n1unEiBC13yO/eizEIHWq+8+OWT",
  9. ];
  10. // 安装事件 - 缓存静态资源
  11. self.addEventListener("install", (event) => {
  12. console.log("Service Worker 安装中...");
  13. event.waitUntil(
  14. caches
  15. .open(CACHE_NAME)
  16. .then((cache) => {
  17. console.log("缓存静态资源");
  18. return cache.addAll(STATIC_CACHE_URLS);
  19. })
  20. .then(() => {
  21. console.log("静态资源缓存完成");
  22. return self.skipWaiting();
  23. })
  24. );
  25. });
  26. // 激活事件 - 清理旧缓存
  27. self.addEventListener("activate", (event) => {
  28. console.log("Service Worker 激活中...");
  29. event.waitUntil(
  30. caches
  31. .keys()
  32. .then((cacheNames) => {
  33. return Promise.all(
  34. cacheNames.map((cacheName) => {
  35. if (cacheName !== CACHE_NAME) {
  36. console.log("删除旧缓存:", cacheName);
  37. return caches.delete(cacheName);
  38. }
  39. })
  40. );
  41. })
  42. .then(() => {
  43. console.log("Service Worker 激活完成");
  44. return self.clients.claim();
  45. })
  46. );
  47. });
  48. // 网络请求拦截 - 缓存优先策略
  49. self.addEventListener("fetch", (event) => {
  50. // 只处理同源请求
  51. if (event.request.url.startsWith(self.location.origin)) {
  52. event.respondWith(
  53. caches
  54. .match(event.request)
  55. .then((response) => {
  56. // 如果在缓存中找到,则返回缓存版本
  57. if (response) {
  58. return response;
  59. }
  60. // 否则,从网络获取资源
  61. return fetch(event.request).then((response) => {
  62. // 检查是否是有效响应
  63. if (
  64. !response ||
  65. response.status !== 200 ||
  66. response.type !== "basic"
  67. ) {
  68. return response;
  69. }
  70. // 克隆响应,因为响应流只能被使用一次
  71. const responseToCache = response.clone();
  72. // 将新资源添加到缓存
  73. caches.open(CACHE_NAME).then((cache) => {
  74. cache.put(event.request, responseToCache);
  75. });
  76. return response;
  77. });
  78. })
  79. .catch(() => {
  80. // 网络请求失败,尝试返回缓存的主页
  81. if (event.request.destination === "document") {
  82. return caches.match("/index.html");
  83. }
  84. })
  85. );
  86. }
  87. });
  88. // 后台同步 - 用于在恢复网络连接时同步数据
  89. self.addEventListener("sync", (event) => {
  90. if (event.tag === "background-sync") {
  91. console.log("执行后台同步");
  92. event.waitUntil(doBackgroundSync());
  93. }
  94. });
  95. function doBackgroundSync() {
  96. // 这里可以添加数据同步逻辑
  97. return Promise.resolve();
  98. }