<\/figure>\n<\/p>\n “short_name”: “Website”,<\/p>\n
“description”: “This is my website converted into a PWA”,<\/p>\n
“start_url”: “\/”,<\/p>\n
“display”: “standalone”,<\/p>\n
“background_color”: “#ffffff”,<\/p>\n
“theme_color”: “#ffffff”,<\/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-512×512.png”,<\/p>\n
“sizes”: “512×512”,<\/p>\n
“type”: “image\/png”<\/p>\n
}<\/p>\n
]<\/p>\n
}<\/p>\n
“`<\/p>\n
\u5728\u4f60\u7684HTML\u6587\u4ef6\u7684`head`\u90e8\u5206\uff0c\u5c06\u4ee5\u4e0b\u4ee3\u7801\u6dfb\u52a0\u81f3`manifest.json`\u6587\u4ef6\uff1a<\/p>\n
“`html<\/p>\n<\/p>\n
“`<\/p>\n
2. \u521b\u5efa\u5e76\u6ce8\u518cService Worker<\/p>\n
Service Worker \u662f\u4e00\u4e2a\u4e0e\u4e3b\u7ebf\u7a0b\uff08\u4e3b\u7ebf\u7a0b\u901a\u5e38\u7528\u4e8e\u5904\u7406UI\u5143\u7d20\uff09\u5206\u79bb\u7684Javascript\u6587\u4ef6\u3002\u7528\u4e8e\u5904\u7406\u7f13\u5b58\u3001\u63a8\u9001\u901a\u77e5\u548c\u540e\u53f0\u540c\u6b65\u64cd\u4f5c\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2aService Worker \u5e76\u5728\u6839\u76ee\u5f55\u4e2d\u6ce8\u518c\u3002<\/p>\n
\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662fReact\u3001Vue\u6216Angular\u7b49\u6846\u67b6\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u73b0\u6709\u7684PWA\u652f\u6301\u5e93\u6765\u751f\u6210\u4e00\u4e2aService Worker\u3002<\/p>\n
\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a `serviceworker.js` \u7684\u65b0\u6587\u4ef6\uff0c\u5e76\u5728\u8be5\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n
“`javascript<\/p>\n
self.addEventListener(“install”, function (event) {<\/p>\n
event.waitUntil(<\/p>\n
caches.open(“websites-cache”).then(function (cache) {<\/p>\n
return cache.addAll([<\/p>\n
“\/”,<\/p>\n
“\/index.html”,<\/p>\n
“\/css\/style.css”,<\/p>\n
“\/js\/main.js”,<\/p>\n
“\/icon\/icon-192×192.png”,<\/p>\n
“\/icon\/icon-512×512.png”,<\/p>\n
]);<\/p>\n
})<\/p>\n
);<\/p>\n
});<\/p>\n
self.addEventListener(“fetch”, function (event) {<\/p>\n
event.respondWith(<\/p>\n
caches<\/p>\n
.match(event.request)<\/p>\n
.then(function (cachedResponse) {<\/p>\n
return cachedResponse || fetch(event.request);<\/p>\n
})<\/p>\n
.catch(function (error) {<\/p>\n
console.log(“Failed to fetch “, event.request, error);<\/p>\n
})<\/p>\n
);<\/p>\n
});<\/p>\n
“`<\/p>\n
\u63a5\u4e0b\u6765\uff0c\u5728\u4f60\u7684 `index.html` \u6587\u4ef6\u7684\u5e95\u90e8\uff0c\u6dfb\u52a0\u4ee5\u4e0b\u811a\u672c\u6765\u6ce8\u518c `serviceworker.js`\uff1a<\/p>\n
“`javascript<\/p>\n
if (“serviceWorker” in navigator) {<\/p>\n
window.addEventListener(“load”, function () {<\/p>\n
navigator.serviceWorker.register(“\/serviceworker.js”).then(<\/p>\n
function (registration) {<\/p>\n
console.log(<\/p>\n
“Service Worker registration successful with scope: “,<\/p>\n
registration.scope<\/p>\n
);<\/p>\n
},<\/p>\n
function (err) {<\/p>\n
console.log(“Service Worker registration failed: “, err);<\/p>\n
}<\/p>\n
);<\/p>\n
});<\/p>\n
}<\/p>\n
“`<\/p>\n
3. \u6d4b\u8bd5\u5e76\u90e8\u7f72<\/p>\n
\u73b0\u5728\u4f60\u5df2\u7ecf\u5c06\u4e00\u4e2a\u73b0\u6709\u7684\u7f51\u7ad9\u8f6c\u6362\u4e3aPWA\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5982Lighthouse\u3001PWABuilder\u6216\u5176\u4ed6\u5de5\u5177\u6d4b\u8bd5\u4f60\u7684PWA\u3002\u786e\u4fdd\u4f60\u7684PWA\u6ee1\u8db3\u8981\u6c42\uff0c\u5e76\u6839\u636e\u9700\u8981\u8fdb\u884c\u4fee\u590d\u3002\u4e4b\u540e\u5c06\u5176\u90e8\u7f72\u5230\u73b0\u6709\u7684Web\u670d\u52a1\u5668\u6216\u4f7f\u7528\u670d\u52a1\u547d\u4ee4\u6765\u751f\u6210\u4e00\u4e2a\u9884\u89c8\u94fe\u63a5\u3002<\/p>\n
\u4e0a\u8ff0\u6b65\u9aa4\u5b8c\u6210\u540e\uff0c\u4f60\u5df2\u7ecf\u5c06\u4e00\u4e2a\u7f51\u7ad9\u6210\u529f\u751f\u6210\u4e3aAPP\u3002\u7528\u6237\u53ef\u4ee5\u5c06\u4f60\u7684\u7f51\u7ad9\u6dfb\u52a0\u5230\u4ed6\u4eec\u7684\u8bbe\u5907\u4e3b\u5c4f\u5e55\u4e0a\uff0c\u5728\u79bb\u7ebf\u72b6\u6001\u4e0b\u8bbf\u95ee\u5e76\u611f\u53d7\u5230\u7c7b\u4f3c\u539f\u751f\u5e94\u7528\u7684\u4f53\u9a8c\u3002\u4f60\u53ef\u4ee5\u7ee7\u7eed\u4f18\u5316\u5e76\u6dfb\u52a0\u66f4\u4e30\u5bcc\u7684\u529f\u80fd\u3001\u5982\u63a8\u9001\u901a\u77e5\u548c\u540e\u53f0\u540c\u6b65\u7b49\u529f\u80fd\u6765\u5b9e\u73b0\u66f4\u597d\u7684\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"
\u5982\u4f55\u5c06\u7f51\u7ad9\u751f\u6210\u4e3aAPP\u6559\u7a0b\uff08\u539f\u7406\u4e0e\u8be6\u7ec6\u4ecb\u7ecd\uff09\u5c06\u7f51\u7ad9\u8f6c\u6362\u6210APP\u610f\u5473\u7740\u5c06\u73b0\u6709\u7684\u67d0\u4e2a\u7f51\u7ad9\u6216Web\u5e94\u7528\u6dfb\u52a0\u5230\u624b\u673a\u4e0a\u5e76\u53ef\u4ee5\u79bb\u7ebf\u6d4f\u89c8\u3002\u8fd9\u79cd\u5e94\u7528\u7c7b\u578b\u88ab\u79f0\u4e3aProgressive Web App\uff08PWA\uff09\uff0c\u4f60\u53ef\u4ee5<\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[27,10198,25,247,15182],"topic":[],"class_list":["post-8848","post","type-post","status-publish","format-standard","hentry","category-appkf","tag-27","tag-app","tag-25","tag-247"],"_links":{"self":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts\/8848","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/comments?post=8848"}],"version-history":[{"count":0,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts\/8848\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/media?parent=8848"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/categories?post=8848"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/tags?post=8848"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/topic?post=8848"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}