{"id":12690,"date":"2023-11-13T18:35:00","date_gmt":"2023-11-13T10:35:00","guid":{"rendered":"https:\/\/www.zhidianwl.com\/zhidian\/?p=12690"},"modified":"2023-11-13T18:35:00","modified_gmt":"2023-11-13T10:35:00","slug":"%e5%a6%82%e4%bd%95%e5%85%8d%e8%b4%b9%e6%8a%8a%e7%bd%91%e7%ab%99%e7%94%9f%e6%88%90app%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.zhidianwl.com\/zhidian\/2023\/12690\/","title":{"rendered":"\u5982\u4f55\u514d\u8d39\u628a\u7f51\u7ad9\u751f\u6210app\uff1f"},"content":{"rendered":"<p>\u5c06\u7f51\u7ad9\u8f6c\u6362\u4e3a\u79fb\u52a8\u5e94\u7528\u7a0b\u5e8f(App)\u53ef\u4ee5\u8ba9\u7528\u6237\u5728\u4e0d\u540c\u7684\u8bbe\u5907\u4e0a\u66f4\u65b9\u4fbf\u5730\u8bbf\u95ee\u60a8\u7684\u7f51\u7ad9\u3002\u8fd9\u79cd\u65b9\u6cd5\u79f0\u4e3aWeb App\u6216Progressive Web App (PWA)\u3002\u4e0b\u9762\u662f\u5c06\u7f51\u7ad9\u514d\u8d39\u8f6c\u6362\u4e3aApp\u7684\u8be6\u7ec6\u6559\u7a0b\u3002<\/p>\n<p>\u4e00\u3001\u4e86\u89e3Web App\u548cPWA\u7684\u6982\u5ff5\u4e0e\u4f18\u52bf<\/p>\n<p>1. Web App<\/p>\n<p>Web App\u662f\u6307\u901a\u8fc7\u6d4f\u89c8\u5668\u8bbf\u95ee\u7684\u3001\u7528\u6237\u4ea4\u4e92\u6027\u5f3a\u3001\u529f\u80fd\u4e30\u5bcc\u4ee5\u53ca\u6709\u7279\u5b9a\u76ee\u6807\u7528\u9014\u7684\u5e94\u7528\u7a0b\u5e8f\u3002Web App\u4e0e\u666e\u901a\u7f51\u7ad9\u7684\u533a\u522b\u5728\u4e8e\uff0c\u5b83\u66f4\u50cf\u662f\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\uff0c\u5177\u6709\u72ec\u7acb\u7684\u529f\u80fd\u800c\u4e0d\u4ec5\u4ec5\u662f\u5c55\u793a\u4fe1\u606f\u3002<\/p>\n<p>2. Progressive Web App (PWA)<\/p>\n<p>PWA\u662fWeb App\u7684\u5347\u7ea7\u7248\uff0c\u901a\u8fc7\u6dfb\u52a0\u65b0\u7684\u529f\u80fd\u548c\u4f18\u5316\uff0c\u4f7f\u5f97Web App\u5177\u6709\u4e0e\u539f\u751f\u5e94\u7528<\/p>\n<p><figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i.cdn.yimenapp.com\/2023-01\/06\/16\/05137844178.jpg\" \/><\/figure>\n<\/p>\n<p>\u76f8\u8fd1\u7684\u7528\u6237\u4f53\u9a8c\u3002PWA\u5177\u6709\u54cd\u5e94\u5f0f\u8bbe\u8ba1\u3001\u79bb\u7ebf\u8bbf\u95ee\u3001\u63a8\u9001\u901a\u77e5\u7b49\u7279\u70b9\uff0c\u53ef\u4ee5\u76f4\u63a5\u6dfb\u52a0\u5230\u624b\u673a\u4e3b\u5c4f\u5e55\uff0c\u65e0\u9700\u4ece\u5e94\u7528\u5546\u5e97\u4e0b\u8f7d\u3002<\/p>\n<p>\u4e8c\u3001\u5c06\u7f51\u7ad9\u8f6c\u6362\u4e3aPWA<\/p>\n<p>1. \u51c6\u5907\u5de5\u4f5c<\/p>\n<p>\u786e\u4fdd\u4f60\u7684\u7f51\u7ad9\u5177\u5907\u4ee5\u4e0b\u4e00\u4e9b\u57fa\u672c\u7279\u6027\uff1a<\/p>\n<p>  a. \u81ea\u9002\u5e94\u548c\u54cd\u5e94\u5f0f\u8bbe\u8ba1\uff0c\u9002\u5e94\u4e0d\u540c\u8bbe\u5907\u7684\u5c4f\u5e55\u5c3a\u5bf8\uff1b<\/p>\n<p>  b. \u901a\u8fc7HTTPS\u8fde\u63a5\u8bbf\u95ee\uff0c\u786e\u4fdd\u7f51\u7ad9\u5b89\u5168\uff1b<\/p>\n<p>  c. \u5feb\u901f\u52a0\u8f7d\uff0c\u4f18\u5316\u9875\u9762\u5185\u5bb9\u3001\u56fe\u7247\u548c\u811a\u672c\u7b49\uff0c\u63d0\u9ad8\u52a0\u8f7d\u901f\u5ea6\u3002<\/p>\n<p>2. \u521b\u5efa\u4e00\u4e2amanifest.json\u6587\u4ef6<\/p>\n<p>\u8fd9\u662f\u4e00\u4e2a\u914d\u7f6e\u6587\u4ef6\uff0c\u7528\u4e8e\u544a\u77e5\u6d4f\u89c8\u5668\u6709\u5173PWA\u7684\u76f8\u5173\u4fe1\u606f\uff0c\u5305\u62ec\u540d\u79f0\u3001\u56fe\u6807\u3001\u63cf\u8ff0\u7b49\u3002\u53ef\u4ee5\u4f7f\u7528\u5728\u7ebf\u5de5\u5177\uff08\u5982 https:\/\/app-manifest.firebaseapp.com\/ \uff09\u751f\u6210\u8fd9\u4e2a\u6587\u4ef6\uff0c\u7136\u540e\u5c06\u5176\u653e\u7f6e\u4e8e\u7f51\u7ad9\u6839\u76ee\u5f55\u4e0b\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`json<\/p>\n<p>{<\/p>\n<p>  &#8220;name&#8221;: &#8220;My Website&#8221;,<\/p>\n<p>  &#8220;short_name&#8221;: &#8220;Website&#8221;,<\/p>\n<p>  &#8220;description&#8221;: &#8220;A progressive web app example&#8221;,<\/p>\n<p>  &#8220;start_url&#8221;: &#8220;\/&#8221;,<\/p>\n<p>  &#8220;display&#8221;: &#8220;standalone&#8221;,<\/p>\n<p>  &#8220;background_color&#8221;: &#8220;#ffffff&#8221;,<\/p>\n<p>  &#8220;theme_color&#8221;: &#8220;#3f51b5&#8221;,<\/p>\n<p>  &#8220;icons&#8221;: [<\/p>\n<p>    {<\/p>\n<p>      &#8220;src&#8221;: &#8220;icon-192&#215;192.png&#8221;,<\/p>\n<p>      &#8220;sizes&#8221;: &#8220;192&#215;192&#8221;,<\/p>\n<p>      &#8220;type&#8221;: &#8220;image\/png&#8221;<\/p>\n<p>    },<\/p>\n<p>    {<\/p>\n<p>      &#8220;src&#8221;: &#8220;icon-512&#215;512.png&#8221;,<\/p>\n<p>      &#8220;sizes&#8221;: &#8220;512&#215;512&#8221;,<\/p>\n<p>      &#8220;type&#8221;: &#8220;image\/png&#8221;<\/p>\n<p>    }<\/p>\n<p>  ]<\/p>\n<p>}<\/p>\n<p>&#8220;`<\/p>\n<p>3. \u6dfb\u52a0manifest.json\u5f15\u7528<\/p>\n<p>\u5728\u7f51\u9875\u7684head\u6807\u7b7e\u4e2d\uff0c\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\u5f15\u7528manifest.json\u6587\u4ef6\uff1a<\/p>\n<p>&#8220;`html<\/p>\n<\/p>\n<p>&#8220;`<\/p>\n<p>4. \u8bf7\u6c42\u63a8\u9001\u901a\u77e5\u6743\u9650\uff08\u53ef\u9009\uff09<\/p>\n<p>\u5982\u679c\u60a8\u5e0c\u671b\u5411\u7528\u6237\u53d1\u9001\u63a8\u9001\u901a\u77e5\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0bJavaScript\u4ee3\u7801\u6765\u8bf7\u6c42\u6743\u9650\uff1a<\/p>\n<p>&#8220;`javascript<\/p>\n<p>Notification.requestPermission().then(function (permission) {<\/p>\n<p>  if (permission === &#8216;granted&#8217;) {<\/p>\n<p>    consol<a href=\"http:\/\/www.yimenyun.cn\/app-shengcheng-gongju-484.html\">\u5e94\u7528\u751f\u6210\u5728\u7ebf\u7f51\u7ad9app<\/a>e.log(&#8216;Notification permission granted.&#8217;);<\/p>\n<p>  } else {<\/p>\n<p>    console.log(&#8216;Notification permission denied&#8217;);<\/p>\n<p>  }<\/p>\n<p>});<\/p>\n<p>&#8220;`<\/p>\n<p>5. \u6ce8\u518c\u4e00\u4e2aService Worker<\/p>\n<p>Service Worker\u662f\u4e00\u4e2a\u652f\u6301\u79bb\u7ebf\u8bbf\u95ee\u548c\u63a8\u9001\u901a\u77e5\u7684\u5173\u952e\u529f\u80fd\u3002\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aservice-worker.js\u7684\u6587\u4ef6\uff0c\u5c06\u5176\u653e\u7f6e\u5728\u7f51\u7ad9\u6839\u76ee\u5f55\u4e0b\u3002\u5728\u60a8\u7684\u4e3bJavaScript\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\u6765\u6ce8\u518cService Worker\uff1a<\/p>\n<p>&#8220;`javascript<\/p>\n<p>if (&#8216;serviceWorker&#8217; in navigator) {<\/p>\n<p>  navigator.serviceWorker.register<a href=\"http:\/\/www.yimenyun.cn\/app-shengcheng-gongju-143.html\">\u7f51\u7ad9\u751f\u6210app\u7684\u4e00\u4e2a\u8f6f\u4ef6<\/a>(&#8216;\/service-worker.js&#8217;)<\/p>\n<p>    .then(function (registration) {<\/p>\n<p>      console.log(&#8216;Service Worker registered&#8217;);<\/p>\n<p>    })<\/p>\n<p>    .catch(function (error) {<\/p>\n<p>      console.log(&#8216;Service Worker registration failed:&#8217;, error);<\/p>\n<p>    });<\/p>\n<p>}<\/p>\n<p>&#8220;`<\/p>\n<p>6. \u7f16\u5199Service Worker\u5185\u5bb9<\/p>\n<p>\u5728service-worker.js\u6587\u4ef6\u4e2d\u7f16\u5199\u4ee3\u7801\uff0c\u5b9e\u73b0\u79bb\u7ebf\u8bbf\u95ee\u3001\u7f13\u5b58\u7b49\u529f\u80fd\uff1a<\/p>\n<p>&#8220;`javascript<\/p>\n<p>self.addEventListener(&#8216;install&#8217;, function (event) {<\/p>\n<p>  event.waitUntil(<\/p>\n<p>    caches.open(&#8216;my-cache&#8217;).then(function (cache) {<\/p>\n<p>      return cache.addAll([<\/p>\n<p>        &#8216;\/&#8217;,<\/p>\n<p>        &#8216;\/index.html&#8217;,<\/p>\n<p>        &#8216;\/style.css&#8217;,<\/p>\n<p>        &#8216;\/icon-192&#215;192.png&#8217;,<\/p>\n<p>        &#8216;\/icon-512&#215;512.png&#8217;<\/p>\n<p>      ]);<\/p>\n<p>    })<\/p>\n<p>  );<\/p>\n<p>});<\/p>\n<p>self.addEventListener(&#8216;fetch&#8217;, function (event) {<\/p>\n<p>  event.respondWith(<\/p>\n<p>    caches.match(event.request).then(function (response) {<\/p>\n<p>      return response || fetch(event.request);<\/p>\n<p>    })<\/p>\n<p>  );<\/p>\n<p>});<\/p>\n<p>self.addEventListener(&#8216;activate&#8217;, function (event) {<\/p>\n<p>  event.waitUntil(<\/p>\n<p>    caches.keys().then(function (keyList) {<\/p>\n<p>      return Promise.all(keyList.map(function (key) {<\/p>\n<p>        if (key !== &#8216;my-cache&#8217;) {<\/p>\n<p>          return caches.delete(key);<\/p>\n<p>        }<\/p>\n<p>      }));<\/p>\n<p>    })<\/p>\n<p>  );<\/p>\n<p>});<\/p>\n<p>&#8220;`<\/p>\n<p>7. \u6d4b\u8bd5\u548c\u4f18\u5316<\/p>\n<p>\u4f7f\u7528\u8c37\u6b4c\u6d4f\u89c8\u5668Chrome\u7684\u5f00\u53d1\u8005\u5de5\u5177\uff08\u5728Chrome\u6d4f\u89c8\u5668\u4e2d\u6309F12\u952e\u6253\u5f00\uff09\u4e2d\u7684\u201cApplication\u201d\u9009\u9879\u5361\u6765\u6d4b\u8bd5PWA\u3002\u6839\u636e\u9700\u8981\u8c03\u8bd5\u548c\u4f18\u5316\u3002<\/p>\n<p>\u73b0\u5728\uff0c\u60a8\u7684\u7f51\u7ad9\u5df2\u6210\u529f\u8f6c\u6362\u4e3aPWA\u3002\u7528\u6237\u53ef\u4ee5\u76f4\u63a5\u6dfb\u52a0\u5230\u624b\u673a\u4e3b\u5c4f\u5e55\uff0c\u5728\u79bb\u7ebf\u65f6\u8bbf\u95ee\uff0c\u5e76\u63a5\u6536\u63a8\u9001\u901a\u77e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5c06\u7f51\u7ad9\u8f6c\u6362\u4e3a\u79fb\u52a8\u5e94\u7528\u7a0b\u5e8f(App)\u53ef\u4ee5\u8ba9\u7528\u6237\u5728\u4e0d\u540c\u7684\u8bbe\u5907\u4e0a\u66f4\u65b9\u4fbf\u5730\u8bbf\u95ee\u60a8\u7684\u7f51\u7ad9\u3002\u8fd9\u79cd\u65b9\u6cd5\u79f0\u4e3aWeb App\u6216Progressive Web App (PWA)\u3002\u4e0b\u9762\u662f\u5c06\u7f51\u7ad9\u514d\u8d39\u8f6c\u6362\u4e3aApp\u7684\u8be6\u7ec6\u6559\u7a0b<\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[10741,186,82,76,10071],"class_list":["post-12690","post","type-post","status-publish","format-standard","hentry","category-zuoapp","tag-html--app","tag-186","tag-82","tag-76","tag-app"],"_links":{"self":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/12690","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/comments?post=12690"}],"version-history":[{"count":0,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/12690\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/media?parent=12690"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/categories?post=12690"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/tags?post=12690"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}