{"id":14781,"date":"2024-01-05T18:32:09","date_gmt":"2024-01-05T10:32:09","guid":{"rendered":"https:\/\/www.yimenyun.cn\/emen\/?p=14781"},"modified":"2024-01-05T18:32:09","modified_gmt":"2024-01-05T10:32:09","slug":"%e6%9c%89%e4%bc%81%e4%b8%9a%e7%bd%91%e7%ab%99%e6%80%8e%e4%b9%88%e7%94%9f%e6%88%90app%e7%bd%91%e7%ab%99%e5%91%a2%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.yimenyun.cn\/emen\/14781\/","title":{"rendered":"\u6709\u4f01\u4e1a\u7f51\u7ad9\u600e\u4e48\u751f\u6210app\u7f51\u7ad9\u5462\uff1f"},"content":{"rendered":"
\u5f53\u4f60\u62e5\u6709\u4e00\u4e2a\u4f01\u4e1a\u7f51\u7ad9\uff0c\u5c06\u5176\u8f6c\u6362\u6210\u4e00\u4e2aAPP\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u63d0\u9ad8\u7528\u6237\u4f53\u9a8c\u5e76\u4e14\u589e\u52a0\u54c1\u724c\u4ef7\u503c\u3002\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u6839\u636eWeb\u6280\u672f\u5236\u4f5cAPP\u3002\u4e0b\u9762\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4e00\u79cd\u53eb\u505a”\u6e10\u8fdb\u5f0f\u7f51\u9875\u5e94\u7528”\uff08Progressive Web App, \u7b80\u79f0PWA\uff09\u7684\u6280\u672f\u3002<\/p>\n
1. \u4ec0\u4e48\u662fPWA\uff1f<\/p>\n
PWA\u662f\u4e00\u79cd\u53ef\u4ee5\u8ba9\u4f60\u7684\u7f51\u7ad9\u5177\u5907\u539f\u751fAPP\u7279\u6027\u7684\u6280\u672f\u3002\u5b83\u5177\u6709\u4e00\u4e9b\u663e\u8457\u7684\u4f18\u70b9\uff0c\u4f8b\u5982\u53ef\u79bb\u7ebf\u8bbf\u95ee\u3001\u7c7b\u4f3cAPP\u7684UI\u3001\u81ea\u52a8\u66f4\u65b0\u7b49\u3002\u8fd9\u4e00\u5207\u90fd\u662f\u901a\u8fc7\u4f7f\u7528\u540d\u4e3aService Worker\uff08\u670d\u52a1\u5de5\u4f5c\u8005\uff09\u7684\u6280\u672f\u5b9e\u73b0\u7684\u3002\u4e0e\u4f20\u7edfAPP\u76f8\u6bd4\uff0cPWA\u5177\u6709\u66f4\u5feb\u7684\u52a0\u8f7d\u901f\u5ea6\uff0c\u66f4\u5c11\u7684\u6570\u636e\u6d88\u8017\uff0c\u5e76\u4e14\u4e0d\u9700\u8981\u63d0\u4ea4\u5230\u5e94\u7528\u5546\u5e97\u3002<\/p>\n
2. \u600e\u4e48\u628a\u7f51\u7ad9\u8f6c\u6210PWA APP\uff1f<\/p>\n
(1) \u5f00\u53d1HTTPS\u8fde\u63a5<\/p>\n
\u9996\u5148\u786e\u4fdd\u4f60\u7684\u7f51\u7ad9\u90e8\u7f72\u5728\u652f\u6301HTTPS\u7684\u670d\u52a1\u5668\u4e0a\u3002\u8fd9\u662f\u5fc5\u987b\u7684\u56e0\u4e3aPWA\u4f7f\u7528\u6d89\u53ca\u5230\u7528\u6237\u6570\u636e\u5b89\u5168\u7684\u529f\u80fd\uff0c\u53ea\u6709\u5728HTTPS\u4e0b\u624d\u80fd\u5f00\u542f\u3002<\/p>\n
(2) \u521b\u5efaWeb App Manifest<\/p>\n
Web App Manifest\u662f\u4e00\u4e2a\u5305\u542b\u7f51\u7ad9\u76f8\u5173\u4fe1\u606f\u7684JSON\u6587\u4ef6\uff0c\u5982\u540d\u79f0\u3001\u56fe\u6807\u3001\u63cf\u8ff0\u3001\u542f\u52a8\u9875\u3001\u5c4f\u5e55\u65b9\u5411\u7b49\u3002\u521b\u5efa\u4e00\u4e2amanifest.json\u6587\u4ef6\u5e76\u5728\u7f51\u9875\u4e2d\u5f15\u7528\u5b83\u3002<\/p>\n
\u793a\u4f8b\uff1amanifest.json<\/p>\n
“`json<\/p>\n
{<\/p>\n
“name”: “YourAppName”,<\/p>\n
“short_name”: “AppName”,<\/p>\n
“description”: “A description of your app”,<\/p>\n
“start_url”: “\/”,<\/p>\n
“display”: “standalone”,<\/p>\n
“background_color”: “#ffffff”,<\/p>\n
“theme_color”: “#000000”,<\/p>\n
“icons”: [<\/p>\n
{<\/p>\n
“src”: “\/icons\/icon-192×192.png”,<\/p>\n
“sizes”: “192×192”,<\/p>\n
“type”: “image\/png”<\/p>\n
},<\/p>\n
{<\/p>\n
“src”: “\/icons\/icon-256×256.png”,<\/p>\n
“sizes<\/p>\n
“: “256×256”,<\/p>\n “type”: “image\/png”<\/p>\n },<\/p>\n {<\/p>\n “src”: “\/icons\/icon-512×512.png”,<\/p>\n “sizes”: “512×512”,<\/p>\n “type”: “image\/png”<\/p>\n }<\/p>\n ]<\/p>\n }<\/p>\n “`<\/p>\n (3) \u6dfb\u52a0Service Worker<\/p>\n \u521b\u5efa\u4e00\u4e2ajs\u6587\u4ef6\uff08\u4f8b\u5982\uff1asw.js\uff09\uff0c\u8fd9\u662fservice worker\u7684\u6838\u5fc3\u3002\u5728\u8fd9\u4e2a\u6587\u4ef6\u4e2d\uff0c\u4f60\u9700\u8981\u7f16\u5199\u7f13\u5b58\u7b56\u7565\u3001\u54cd\u5e94\u79bb\u7ebf\u8bf7\u6c42\u3001\u81ea\u52a8\u66f4\u65b0\u7b49\u529f\u80fd\u3002<\/p>\n \u793a\u4f8b\uff1a\u5728\u4e3b\u6587\u4ef6\u4e2d\u6ce8\u518cService Worker\uff1a<\/p>\n “`javascript<\/p>\n if (‘serviceWorker’ in navigator) {<\/p>\n navigator.serviceWorker.register(‘\/sw.js’).then(function(registration) {<\/p>\n console.log(‘Service Worker registered with scope:’, registration.scope);<\/p>\n }).catch(function(error) {<\/p>\n console.log(‘Service Worker registration failed:’, error);<\/p>\n });<\/p>\n }<\/p>\n “`<\/p>\n (4) \u914d\u7f6eService Worker<\/p>\n \u5728sw.js\u6587\u4ef6\u4e2d\uff0c\u4f60\u9700\u8981\u5b9a\u4e49\u7f13\u5b58\u7b56\u7565\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u7684\u793a\u4f8b\uff1a<\/p>\n “`javascript<\/p>\n self.addEventListener(‘install’, function(event) {<\/p>\n event.waitUntil(<\/p>\n caches.open(‘your-cache-name’).then(function(cache) {<\/p>\n<\/figure>\n<\/p>\n