Supercharge Your Website Using PWA: Installable Website What is a PWA? Why use PWA? Getting Started Project using this Implementation Smartsapp Reference Thanks for reading
{"name":"<name of the application>","short_name":"<short name for the application> (can be same as name)","start_url":"<start url for the website>","display":"<display mode for the website>","description":"<description of the application>","background_color":"<color>","theme_color":"<color>","orientation":"<orientation>","icons":[{"src":"<image source>","sizes":"<widthxheight>","type":"image/png"}]}
一个例子manifest.json如下
{"name":"PWA: Installable website","short_name":"Installable PWA","start_url":"index.html","display":"standalone","description":"App for testing PWA features","background_color":"#ffffff","theme_color":"#000000","orientation":"portrait-primary","icons":[{"src":"image/icon-24.png","sizes":"24x24","type":"image/png"},{"src":"image/icon-32.png","sizes":"32x32","type":"image/png"},{"src":"image/icon-48.png","sizes":"48x48","type":"image/png"},{"src":"image/icon-64.png","sizes":"64x64","type":"image/png"},{"src":"image/icon-128.png","sizes":"128x128","type":"image/png"},{"src":"image/icon-256.png","sizes":"256x256","type":"image/png"}]}
constSTATIC_CACHE="static-cache-v1"conststatic_assets=["/","/index.html","/script.js","/image/icon-24.png","/image/icon-32.png","/image/icon-48.png","/image/icon-64.png","/image/icon-72.png","/image/icon-96.png","/image/icon-128.png","/image/icon-256.png",]// storing static assets in cache on service worker installself.addEventListener("install",event=>{event.waitUntil(caches.open(STATIC_CACHE).then(cache=>{cache.addAll(static_assets)}))})// returning static assets from cacheself.addEventListener("fetch",event=>{event.respondWith(caches.match(event.request).then(response=>{returnresponse||fetch(event.request);}))});
我们需要处理该fetch事件以启用安装。
service worker通过在网站中添加以下脚本来启用
<script>if ('serviceWorker'innavigator){navigator.serviceWorker.register('/service-worker.js');}else{console.log("Service worker is not supported");}</script>