{"id":315,"date":"2026-11-13T12:04:01","date_gmt":"2026-11-13T04:04:01","guid":{"rendered":"https:\/\/yykcj.com\/?p=315"},"modified":"2026-11-13T12:04:01","modified_gmt":"2026-11-13T04:04:01","slug":"service-worker-%e4%b8%8e-pwa-%e5%ae%8c%e6%95%b4%e6%8c%87%e5%8d%97%ef%bc%9a%e4%bb%8e%e7%a6%bb%e7%ba%bf%e7%bc%93%e5%ad%98%e5%88%b0%e6%8e%a8%e9%80%81%e9%80%9a%e7%9f%a5","status":"publish","type":"post","link":"https:\/\/yykcj.com\/?p=315","title":{"rendered":"Service Worker \u4e0e PWA \u5b8c\u6574\u6307\u5357\uff1a\u4ece\u79bb\u7ebf\u7f13\u5b58\u5230\u63a8\u9001\u901a\u77e5"},"content":{"rendered":"<h2>\u4e3a\u4ec0\u4e48\u9700\u8981 PWA \u548c Service Worker\uff1f<\/h2>\n<p>\u5728\u79fb\u52a8\u4f18\u5148\u7684\u65f6\u4ee3\uff0c\u7528\u6237\u671f\u671b Web \u5e94\u7528\u80fd\u50cf\u539f\u751f\u5e94\u7528\u4e00\u6837\u5feb\u901f\u3001\u53ef\u9760\uff0c\u751a\u81f3\u79bb\u7ebf\u53ef\u7528\u3002PWA\uff08Progressive Web App\uff09\u901a\u8fc7\u4e00\u7cfb\u5217\u6280\u672f\uff08\u5176\u4e2d Service Worker \u662f\u6838\u5fc3\uff09\u8ba9 Web \u5e94\u7528\u5177\u5907\u79bb\u7ebf\u80fd\u529b\u3001\u63a8\u9001\u901a\u77e5\u548c\u540e\u53f0\u540c\u6b65\u7b49\u7279\u6027\u3002Service Worker \u662f\u4e00\u4e2a\u8fd0\u884c\u5728\u6d4f\u89c8\u5668\u540e\u53f0\u7684\u811a\u672c\uff0c\u72ec\u7acb\u4e8e\u4e3b\u7ebf\u7a0b\uff0c\u53ef\u4ee5\u62e6\u622a\u7f51\u7edc\u8bf7\u6c42\u3001\u7ba1\u7406\u7f13\u5b58\uff0c\u4ece\u800c\u5b9e\u73b0\u79bb\u7ebf\u4f53\u9a8c\u3002<\/p>\n<p>\u672c\u6587\u5c06\u4ece\u96f6\u5f00\u59cb\uff0c\u5e26\u4f60\u5b9e\u73b0\u4e00\u4e2a\u5b8c\u6574\u7684 PWA\uff0c\u5305\u62ec Service Worker \u7684\u6ce8\u518c\u3001\u7f13\u5b58\u7b56\u7565\u3001\u79bb\u7ebf\u9875\u9762\u3001\u540e\u53f0\u540c\u6b65\u548c\u63a8\u9001\u901a\u77e5\u3002\u6bcf\u4e2a\u6b65\u9aa4\u90fd\u63d0\u4f9b\u53ef\u8fd0\u884c\u7684\u4ee3\u7801\uff0c\u5e76\u5206\u4eab\u5b9e\u9645\u5f00\u53d1\u4e2d\u7684\u6700\u4f73\u5b9e\u8df5\u548c\u8e29\u5751\u7ecf\u9a8c\u3002<\/p>\n<h2>1. \u9879\u76ee\u51c6\u5907<\/h2>\n<p>\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u9759\u6001\u7ad9\u70b9\u4f5c\u4e3a\u793a\u4f8b\u3002\u76ee\u5f55\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<pre><code>\nmy-pwa\/\n\u251c\u2500\u2500 index.html\n\u251c\u2500\u2500 styles.css\n\u251c\u2500\u2500 app.js\n\u251c\u2500\u2500 sw.js\n\u251c\u2500\u2500 manifest.json\n\u2514\u2500\u2500 offline.html\n<\/code><\/pre>\n<p><code>index.html<\/code> \u5305\u542b\u57fa\u672c\u5185\u5bb9\u548c\u6ce8\u518c Service Worker \u7684\u811a\u672c\uff1a<\/p>\n<pre><code class=\"language-html\">\n&lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;zh-CN&quot;&gt;\n&lt;head&gt;\n  &lt;meta charset=&quot;UTF-8&quot;&gt;\n  &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;\n  &lt;title&gt;My PWA&lt;\/title&gt;\n  &lt;link rel=&quot;manifest&quot; href=&quot;\/manifest.json&quot;&gt;\n  &lt;link rel=&quot;stylesheet&quot; href=&quot;\/styles.css&quot;&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n  &lt;h1&gt;\u6b22\u8fce\u4f7f\u7528 PWA \u793a\u4f8b&lt;\/h1&gt;\n  &lt;p&gt;\u5c1d\u8bd5\u79bb\u7ebf\u8bbf\u95ee\u6b64\u9875\u9762\u3002&lt;\/p&gt;\n  &lt;script src=&quot;\/app.js&quot;&gt;&lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n<p><code>app.js<\/code> \u8d1f\u8d23\u6ce8\u518c Service Worker\uff1a<\/p>\n<pre><code class=\"language-javascript\">\nif (&#x27;serviceWorker&#x27; in navigator) {\n  window.addEventListener(&#x27;load&#x27;, () =&gt; {\n    navigator.serviceWorker.register(&#x27;\/sw.js&#x27;)\n      .then(registration =&gt; {\n        console.log(&#x27;Service Worker \u6ce8\u518c\u6210\u529f:&#x27;, registration.scope);\n      })\n      .catch(error =&gt; {\n        console.error(&#x27;Service Worker \u6ce8\u518c\u5931\u8d25:&#x27;, error);\n      });\n  });\n}\n<\/code><\/pre>\n<p><strong>\u6ce8\u610f<\/strong>\uff1aService Worker \u8981\u6c42 HTTPS \u73af\u5883\uff08localhost \u9664\u5916\uff09\uff0c\u786e\u4fdd\u4f60\u7684\u5f00\u53d1\u670d\u52a1\u5668\u652f\u6301 HTTPS \u6216\u4f7f\u7528 localhost\u3002<\/p>\n<h2>2. \u521b\u5efa Web App Manifest<\/h2>\n<p><code>manifest.json<\/code> \u5b9a\u4e49\u5e94\u7528\u7684\u5916\u89c2\u548c\u884c\u4e3a\uff0c\u662f PWA \u53ef\u5b89\u88c5\u6027\u7684\u5173\u952e\uff1a<\/p>\n<pre><code class=\"language-json\">\n{\n  &quot;name&quot;: &quot;My PWA&quot;,\n  &quot;short_name&quot;: &quot;PWA&quot;,\n  &quot;start_url&quot;: &quot;\/&quot;,\n  &quot;display&quot;: &quot;standalone&quot;,\n  &quot;background_color&quot;: &quot;#ffffff&quot;,\n  &quot;theme_color&quot;: &quot;#3f51b5&quot;,\n  &quot;icons&quot;: [\n    {\n      &quot;src&quot;: &quot;\/icons\/icon-192x192.png&quot;,\n      &quot;sizes&quot;: &quot;192x192&quot;,\n      &quot;type&quot;: &quot;image\/png&quot;\n    },\n    {\n      &quot;src&quot;: &quot;\/icons\/icon-512x512.png&quot;,\n      &quot;sizes&quot;: &quot;512x512&quot;,\n      &quot;type&quot;: &quot;image\/png&quot;\n    }\n  ]\n}\n<\/code><\/pre>\n<p>\u786e\u4fdd\u56fe\u6807\u6587\u4ef6\u5b58\u5728\uff0c\u5426\u5219\u5b89\u88c5\u63d0\u793a\u53ef\u80fd\u4e0d\u4f1a\u51fa\u73b0\u3002<\/p>\n<h2>3. Service Worker \u751f\u547d\u5468\u671f\u4e0e\u7f13\u5b58\u7b56\u7565<\/h2>\n<p>Service Worker \u7684\u751f\u547d\u5468\u671f\u5305\u62ec\u5b89\u88c5\uff08install\uff09\u3001\u6fc0\u6d3b\uff08activate\uff09\u548c\u7a7a\u95f2\uff08idle\uff09\u9636\u6bb5\u3002\u5728 <code>install<\/code> \u4e8b\u4ef6\u4e2d\u9884\u7f13\u5b58\u5173\u952e\u8d44\u6e90\uff0c\u5728 <code>activate<\/code> \u4e8b\u4ef6\u4e2d\u6e05\u7406\u65e7\u7f13\u5b58\u3002<\/p>\n<p>\u521b\u5efa <code>sw.js<\/code>\uff1a<\/p>\n<pre><code class=\"language-javascript\">\nconst CACHE_NAME = &#x27;my-pwa-cache-v1&#x27;;\nconst PRECACHE_URLS = [\n  &#x27;\/&#x27;,\n  &#x27;\/index.html&#x27;,\n  &#x27;\/styles.css&#x27;,\n  &#x27;\/app.js&#x27;,\n  &#x27;\/offline.html&#x27;\n];\n\n\/\/ \u5b89\u88c5\u9636\u6bb5\uff1a\u9884\u7f13\u5b58\u8d44\u6e90\nself.addEventListener(&#x27;install&#x27;, event =&gt; {\n  event.waitUntil(\n    caches.open(CACHE_NAME)\n      .then(cache =&gt; cache.addAll(PRECACHE_URLS))\n      .then(() =&gt; self.skipWaiting())\n  );\n});\n\n\/\/ \u6fc0\u6d3b\u9636\u6bb5\uff1a\u6e05\u7406\u65e7\u7f13\u5b58\nself.addEventListener(&#x27;activate&#x27;, event =&gt; {\n  event.waitUntil(\n    caches.keys().then(cacheNames =&gt; {\n      return Promise.all(\n        cacheNames.map(cacheName =&gt; {\n          if (cacheName !== CACHE_NAME) {\n            return caches.delete(cacheName);\n          }\n        })\n      );\n    }).then(() =&gt; self.clients.claim())\n  );\n});\n<\/code><\/pre>\n<p><strong>\u6700\u4f73\u5b9e\u8df5<\/strong>\uff1a\u4f7f\u7528 <code>skipWaiting()<\/code> \u548c <code>clients.claim()<\/code> \u8ba9\u65b0 Service Worker \u7acb\u5373\u63a5\u7ba1\uff0c\u4f46\u8981\u6ce8\u610f\u8fd9\u53ef\u80fd\u5bfc\u81f4\u9875\u9762\u8d44\u6e90\u7248\u672c\u4e0d\u4e00\u81f4\uff0c\u5efa\u8bae\u5728\u66f4\u65b0\u65f6\u63d0\u793a\u7528\u6237\u5237\u65b0\u3002<\/p>\n<h2>4. \u5b9e\u73b0\u7075\u6d3b\u7684\u7f13\u5b58\u7b56\u7565<\/h2>\n<p>\u4e0d\u540c\u7684\u8d44\u6e90\u9700\u8981\u4e0d\u540c\u7684\u7f13\u5b58\u7b56\u7565\u3002\u5bf9\u4e8e\u9759\u6001\u8d44\u6e90\uff08CSS\u3001JS\u3001\u56fe\u7247\uff09\uff0c\u4f7f\u7528\u201c\u7f13\u5b58\u4f18\u5148\uff0c\u7f51\u7edc\u56de\u9000\u201d\uff1b\u5bf9\u4e8e API \u8bf7\u6c42\uff0c\u4f7f\u7528\u201c\u7f51\u7edc\u4f18\u5148\uff0c\u7f13\u5b58\u56de\u9000\u201d\u3002<\/p>\n<p>\u5728 <code>sw.js<\/code> \u4e2d\u6dfb\u52a0 <code>fetch<\/code> \u4e8b\u4ef6\u76d1\u542c\uff1a<\/p>\n<pre><code class=\"language-javascript\">\nself.addEventListener(&#x27;fetch&#x27;, event =&gt; {\n  const { request } = event;\n  const url = new URL(request.url);\n\n  \/\/ \u5bf9\u4e8e\u5bfc\u822a\u8bf7\u6c42\uff0c\u4f7f\u7528\u7f51\u7edc\u4f18\u5148\uff0c\u79bb\u7ebf\u65f6\u56de\u9000\u5230\u79bb\u7ebf\u9875\u9762\n  if (request.mode === &#x27;navigate&#x27;) {\n    event.respondWith(\n      fetch(request).catch(() =&gt; caches.match(&#x27;\/offline.html&#x27;))\n    );\n    return;\n  }\n\n  \/\/ \u5bf9\u4e8e\u9759\u6001\u8d44\u6e90\uff0c\u4f7f\u7528\u7f13\u5b58\u4f18\u5148\n  if (url.origin === location.origin &amp;&amp; PRECACHE_URLS.includes(url.pathname)) {\n    event.respondWith(\n      caches.match(request).then(cachedResponse =&gt; {\n        return cachedResponse || fetch(request);\n      })\n    );\n    return;\n  }\n\n  \/\/ \u5bf9\u4e8e\u5176\u4ed6\u8bf7\u6c42\uff08\u5982 API\uff09\uff0c\u7f51\u7edc\u4f18\u5148\uff0c\u5931\u8d25\u65f6\u5c1d\u8bd5\u7f13\u5b58\n  event.respondWith(\n    fetch(request)\n      .then(response =&gt; {\n        \/\/ \u53ef\u4ee5\u5728\u8fd9\u91cc\u7f13\u5b58 API \u54cd\u5e94\n        return response;\n      })\n      .catch(() =&gt; caches.match(request))\n  );\n});\n<\/code><\/pre>\n<p><strong>\u6ce8\u610f<\/strong>\uff1a\u7f13\u5b58 API \u54cd\u5e94\u65f6\u8981\u8c28\u614e\uff0c\u907f\u514d\u7f13\u5b58\u654f\u611f\u6570\u636e\u3002\u53ef\u4ee5\u4f7f\u7528 <code>Cache-Control<\/code> \u5934\u6216\u81ea\u5b9a\u4e49\u903b\u8f91\u51b3\u5b9a\u662f\u5426\u7f13\u5b58\u3002<\/p>\n<h2>5. \u6dfb\u52a0\u79bb\u7ebf\u9875\u9762<\/h2>\n<p>\u521b\u5efa <code>offline.html<\/code>\uff0c\u5f53\u7f51\u7edc\u4e0d\u53ef\u7528\u65f6\u663e\u793a\uff1a<\/p>\n<pre><code class=\"language-html\">\n&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n  &lt;meta charset=&quot;UTF-8&quot;&gt;\n  &lt;title&gt;\u79bb\u7ebf&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n  &lt;h1&gt;\u4f60\u5f53\u524d\u5904\u4e8e\u79bb\u7ebf\u72b6\u6001&lt;\/h1&gt;\n  &lt;p&gt;\u8bf7\u68c0\u67e5\u7f51\u7edc\u8fde\u63a5\u540e\u91cd\u8bd5\u3002&lt;\/p&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n<p>\u786e\u4fdd\u5728 <code>PRECACHE_URLS<\/code> \u4e2d\u5305\u542b\u6b64\u6587\u4ef6\u3002<\/p>\n<h2>6. \u540e\u53f0\u540c\u6b65\uff08Background Sync\uff09<\/h2>\n<p>\u540e\u53f0\u540c\u6b65\u5141\u8bb8\u5728\u79bb\u7ebf\u65f6\u63a8\u8fdf\u64cd\u4f5c\uff0c\u7b49\u7f51\u7edc\u6062\u590d\u540e\u81ea\u52a8\u91cd\u8bd5\u3002\u4f8b\u5982\uff0c\u63d0\u4ea4\u8868\u5355\u3002<\/p>\n<p>\u5728 <code>app.js<\/code> \u4e2d\u6ce8\u518c\u540c\u6b65\uff1a<\/p>\n<pre><code class=\"language-javascript\">\nasync function sendData(data) {\n  try {\n    await fetch(&#x27;\/api\/data&#x27;, {\n      method: &#x27;POST&#x27;,\n      body: JSON.stringify(data),\n      headers: { &#x27;Content-Type&#x27;: &#x27;application\/json&#x27; }\n    });\n  } catch (error) {\n    \/\/ \u7f51\u7edc\u5931\u8d25\uff0c\u6ce8\u518c\u540e\u53f0\u540c\u6b65\n    const registration = await navigator.serviceWorker.ready;\n    await registration.sync.register(&#x27;sync-data&#x27;);\n    \/\/ \u5c06\u6570\u636e\u5b58\u50a8\u5230 IndexedDB\uff0c\u4f9b Service Worker \u4f7f\u7528\n    await storeDataInIndexedDB(data);\n  }\n}\n<\/code><\/pre>\n<p>\u5728 <code>sw.js<\/code> \u4e2d\u76d1\u542c <code>sync<\/code> \u4e8b\u4ef6\uff1a<\/p>\n<pre><code class=\"language-javascript\">\nself.addEventListener(&#x27;sync&#x27;, event =&gt; {\n  if (event.tag === &#x27;sync-data&#x27;) {\n    event.waitUntil(syncData());\n  }\n});\n\nasync function syncData() {\n  const data = await getDataFromIndexedDB();\n  if (data) {\n    await fetch(&#x27;\/api\/data&#x27;, {\n      method: &#x27;POST&#x27;,\n      body: JSON.stringify(data),\n      headers: { &#x27;Content-Type&#x27;: &#x27;application\/json&#x27; }\n    });\n    await clearDataFromIndexedDB();\n  }\n}\n<\/code><\/pre>\n<p><strong>\u8e29\u5751<\/strong>\uff1a\u540e\u53f0\u540c\u6b65\u9700\u8981\u6d4f\u89c8\u5668\u652f\u6301\uff0c\u4e14\u6ce8\u518c\u540c\u6b65\u65f6\u9700\u786e\u4fdd Service Worker \u5df2\u6fc0\u6d3b\u3002\u6570\u636e\u5b58\u50a8\u5efa\u8bae\u4f7f\u7528 IndexedDB\uff0c\u56e0\u4e3a localStorage \u5728 Service Worker \u4e2d\u4e0d\u53ef\u7528\u3002<\/p>\n<h2>7. \u63a8\u9001\u901a\u77e5<\/h2>\n<p>\u63a8\u9001\u901a\u77e5\u9700\u8981\u7528\u6237\u6388\u6743\uff0c\u5e76\u501f\u52a9\u63a8\u9001\u670d\u52a1\uff08\u5982 Firebase Cloud Messaging\uff09\u3002\u8fd9\u91cc\u5c55\u793a\u524d\u7aef\u8ba2\u9605\u6d41\u7a0b\u3002<\/p>\n<p>\u5728 <code>app.js<\/code> \u4e2d\u8bf7\u6c42\u6743\u9650\u5e76\u8ba2\u9605\uff1a<\/p>\n<pre><code class=\"language-javascript\">\nasync function subscribeToPush() {\n  const permission = await Notification.requestPermission();\n  if (permission !== &#x27;granted&#x27;) {\n    console.log(&#x27;\u7528\u6237\u62d2\u7edd\u901a\u77e5&#x27;);\n    return;\n  }\n\n  const registration = await navigator.serviceWorker.ready;\n  const subscription = await registration.pushManager.subscribe({\n    userVisibleOnly: true,\n    applicationServerKey: urlBase64ToUint8Array(&#x27;YOUR_PUBLIC_VAPID_KEY&#x27;)\n  });\n\n  \/\/ \u5c06\u8ba2\u9605\u4fe1\u606f\u53d1\u9001\u5230\u670d\u52a1\u5668\n  await fetch(&#x27;\/api\/subscribe&#x27;, {\n    method: &#x27;POST&#x27;,\n    body: JSON.stringify(subscription),\n    headers: { &#x27;Content-Type&#x27;: &#x27;application\/json&#x27; }\n  });\n}\n\n\/\/ \u8f85\u52a9\u51fd\u6570\uff1a\u5c06 Base64 \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a Uint8Array\nfunction urlBase64ToUint8Array(base64String) {\n  const padding = &#x27;=&#x27;.repeat((4 - base64String.length % 4) % 4);\n  const base64 = (base64String + padding).replace(\/-\/g, &#x27;+&#x27;).replace(\/_\/g, &#x27;\/&#x27;);\n  const rawData = window.atob(base64);\n  const outputArray = new Uint8Array(rawData.length);\n  for (let i = 0; i &lt; rawData.length; ++i) {\n    outputArray[i] = rawData.charCodeAt(i);\n  }\n  return outputArray;\n}\n<\/code><\/pre>\n<p>\u5728 <code>sw.js<\/code> \u4e2d\u76d1\u542c <code>push<\/code> \u4e8b\u4ef6\uff1a<\/p>\n<pre><code class=\"language-javascript\">\nself.addEventListener(&#x27;push&#x27;, event =&gt; {\n  const data = event.data ? event.data.json() : {};\n  const title = data.title || &#x27;\u65b0\u6d88\u606f&#x27;;\n  const options = {\n    body: data.body || &#x27;\u4f60\u6709\u4e00\u6761\u65b0\u901a\u77e5&#x27;,\n    icon: &#x27;\/icons\/icon-192x192.png&#x27;,\n    badge: &#x27;\/icons\/badge.png&#x27;\n  };\n\n  event.waitUntil(\n    self.registration.showNotification(title, options)\n  );\n});\n\nself.addEventListener(&#x27;notificationclick&#x27;, event =&gt; {\n  event.notification.close();\n  event.waitUntil(\n    clients.openWindow(&#x27;\/&#x27;)\n  );\n});\n<\/code><\/pre>\n<p><strong>\u6ce8\u610f<\/strong>\uff1a\u63a8\u9001\u901a\u77e5\u9700\u8981\u670d\u52a1\u5668\u7aef\u914d\u5408\uff0c\u751f\u6210 VAPID \u5bc6\u94a5\u5bf9\uff0c\u5e76\u4f7f\u7528 Web Push \u534f\u8bae\u53d1\u9001\u6d88\u606f\u3002\u751f\u4ea7\u73af\u5883\u4e2d\u5efa\u8bae\u4f7f\u7528\u6210\u719f\u7684\u63a8\u9001\u670d\u52a1\u3002<\/p>\n<h2>8. \u8c03\u8bd5\u4e0e\u66f4\u65b0\u7b56\u7565<\/h2>\n<p>\u4f7f\u7528 Chrome DevTools \u7684 Application \u9762\u677f\u8c03\u8bd5 Service Worker\uff0c\u53ef\u4ee5\u67e5\u770b\u7f13\u5b58\u3001\u66f4\u65b0\u3001\u6ce8\u9500\u7b49\u3002<\/p>\n<p><strong>\u66f4\u65b0\u7b56\u7565<\/strong>\uff1a\u5f53 Service Worker \u6587\u4ef6\u5185\u5bb9\u53d8\u5316\u65f6\uff0c\u6d4f\u89c8\u5668\u4f1a\u4e0b\u8f7d\u65b0\u7248\u672c\u5e76\u5728\u7a7a\u95f2\u65f6\u6fc0\u6d3b\u3002\u4e3a\u4e86\u53ca\u65f6\u66f4\u65b0\uff0c\u53ef\u4ee5\u5728 <code>install<\/code> \u4e8b\u4ef6\u4e2d\u8c03\u7528 <code>skipWaiting()<\/code>\uff0c\u5e76\u5728 <code>activate<\/code> \u4e2d\u8c03\u7528 <code>clients.claim()<\/code>\uff0c\u4f46\u8fd9\u6837\u53ef\u80fd\u5bfc\u81f4\u9875\u9762\u8d44\u6e90\u4e0d\u4e00\u81f4\u3002\u66f4\u53cb\u597d\u7684\u505a\u6cd5\u662f\u76d1\u542c <code>controllerchange<\/code> \u4e8b\u4ef6\uff0c\u63d0\u793a\u7528\u6237\u5237\u65b0\uff1a<\/p>\n<pre><code class=\"language-javascript\">\nlet refreshing = false;\nnavigator.serviceWorker.addEventListener(&#x27;controllerchange&#x27;, () =&gt; {\n  if (refreshing) return;\n  refreshing = true;\n  window.location.reload();\n});\n<\/code><\/pre>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u901a\u8fc7\u672c\u6587\uff0c\u4f60\u5b66\u4f1a\u4e86\u5982\u4f55\u6784\u5efa\u4e00\u4e2a\u5b8c\u6574\u7684 PWA\uff0c\u5305\u62ec Service Worker \u7684\u6ce8\u518c\u3001\u7f13\u5b58\u7b56\u7565\u3001\u79bb\u7ebf\u9875\u9762\u3001\u540e\u53f0\u540c\u6b65\u548c\u63a8\u9001\u901a\u77e5\u3002\u5173\u952e\u70b9\u5728\u4e8e\uff1a\u5408\u7406\u8bbe\u8ba1\u7f13\u5b58\u7b56\u7565\u3001\u5904\u7406 Service Worker \u66f4\u65b0\u3001\u4f7f\u7528 IndexedDB \u5b58\u50a8\u79bb\u7ebf\u6570\u636e\u3002PWA \u80fd\u663e\u8457\u63d0\u5347\u7528\u6237\u4f53\u9a8c\uff0c\u5c24\u5176\u5728\u4e0d\u7a33\u5b9a\u7f51\u7edc\u73af\u5883\u4e0b\u3002<\/p>\n<p>\u4e0b\u4e00\u6b65\uff0c\u4f60\u53ef\u4ee5\u63a2\u7d22\u66f4\u9ad8\u7ea7\u7684\u7f13\u5b58\u5e93\uff08\u5982 Workbox\uff09\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7b56\u7565\u548c\u5de5\u5177\uff0c\u7b80\u5316 Service Worker \u5f00\u53d1\u3002\u540c\u65f6\uff0c\u5173\u6ce8 Web \u5e73\u53f0\u7684\u65b0\u7279\u6027\uff0c\u5982 Periodic Background Sync \u548c Badging API\u3002<\/p>\n<h2>\u5ef6\u4f38\u9605\u8bfb<\/h2>\n<ul>\n<li><a href=\"https:\/\/developer.mozilla.org\/zh-CN\/docs\/Web\/API\/Service<em>Worker<\/em>API&#8221;>Service Worker API &#8211; MDN<\/a><\/li>\n<li><a href=\"https:\/\/developers.google.com\/web\/tools\/workbox\">Workbox \u5b98\u65b9\u6587\u6863<\/a><\/li>\n<li><a href=\"https:\/\/datatracker.ietf.org\/doc\/html\/rfc8030\">Web Push \u534f\u8bae<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u624b\u628a\u624b\u6559\u4f60\u6784\u5efa\u4e00\u4e2a\u5b8c\u6574\u7684 PWA \u5e94\u7528\uff0c\u6db5\u76d6 Service Worker \u751f\u547d\u5468\u671f\u3001\u7f13\u5b58\u7b56\u7565\u3001\u79bb\u7ebf\u4f53\u9a8c\u3001\u540e\u53f0\u540c\u6b65\u548c\u63a8\u9001\u901a\u77e5\u3002\u901a\u8fc7\u5b9e\u9645\u4ee3\u7801\u793a\u4f8b\u548c\u6700\u4f73\u5b9e\u8df5\uff0c\u5e2e\u52a9\u4f60\u63d0\u5347 Web \u5e94\u7528\u7684\u53ef\u9760\u6027\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[47,46,36,252,48],"class_list":["post-315","post","type-post","status-publish","format-standard","hentry","category-3","category-4","tag-pwa","tag-service-worker","tag-36","tag-252","tag-48"],"_links":{"self":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/315","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=315"}],"version-history":[{"count":0,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/315\/revisions"}],"wp:attachment":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}