Upgrade to Pro — share decks privately, control downloads, hide ads and more …

20180827_PWA.pdf

ozaki25
August 27, 2018

 20180827_PWA.pdf

ozaki25

August 27, 2018
Tweet

More Decks by ozaki25

Other Decks in Technology

Transcript

  1. 4FSWJDF8PSLFS <!-- index.html --> <script> if ('serviceWorker' in navigator) {

    navigator.serviceWorker.register(‘/sw.js') .then(registration => { console.log('registration successful'); }).catch(() => { console.log('registration failed'); }); } </script>   // sw.js self.addEventListener('install', event => { console.log('install service worker'); }); self.addEventListener('activate', event => { console.log('activate service worker'); }); self.addEventListener('fetch', event => { console.log('intercept fetch request'); }); 4FSWJDF8PSLFSͷొ࿥ॲཧ
  2. $BDIF"1* const CACHE_NAME = ‘pwa-sample-v1'; const urlsToCache = [ '/assets/css/styles.css',

    '/assets/js/main.js', ]; self.addEventListener('install', event => { event.waitUntil( caches .open(CACHE_NAME) .then(cache => cache.addAll(urlsToCache)) .catch(console.log) ); });   self.addEventListener('fetch', event => { event.respondWith( caches.match(event.request).then(res => { if (res) { console.log('exist in cache'); // cacheʹ͋ͬͨΒͦΕΛฦ͢ return res; } else { console.log('not exist in cache'); // cacheʹͳ͚Ε͹௨৴ͯͦ͠ͷ݁ՌΛฦ͢ return fetch(event.request); } }) ); }); ىಈ࣌ʹ$BDIFʹอଘ $BDIFʹ͋Ε͹௨৴͠ͳ͍
  3. 8FC"QQ.BOJGFTU   NBOJGFTUϑΝΠϧΛ࡞ͬͯIUNMͰಡΈࠐΉ͚ͩ // manifest.json { "name": “React Handson",

    "short_name": "ReactHandson", "start_url": "/", "display": “standalone", "orientation": "any", "background_color": "#f5fcff", "theme_color": "#222222", "icons": [ { "src": "/assets/img/icon_072.png", "sizes": "72x72", "type": "image/png" } ] } <!— index.html —> <link rel="manifest" href=“/manifest.json">
  4. 8FC"QQ.BOJGFTU   Πϯετʔϧϓϩϯϓτͷ੍ޚ // main.js let deferredPrompt; const button

    = document.getElementById('install-button'); // Chrome͕ग़͢ϓϩϯϓτΛ཈ࢭ͠ୀආ͓ͯ͘͠ addEventListener('beforeinstallprompt', function(e) { e.preventDefault(); deferredPrompt = e; button.disabled = false; return false; }); // ϘλϯΛΫϦοΫͨ͠Βୀආ͓͍ͯͨ͠ϓϩϯϓτදࣔॲཧΛ࣮ߦ button.addEventListener('click', function() { if(deferredPrompt !== undefined) { deferredPrompt.prompt(); deferredPrompt = null; } });
  5. ͦͷଞ w 1VTI"1* w ϓογϡ௨஌ w #BDLHSPVOE4ZOD"1* w όοΫάϥ΢ϯυಉظ w

    8FC"VUIFOUJDBUJPO"1* w ೝূͷࣗಈԽ   w 1BZNFOU3FRVFTU"1* w ܾࡁը໘ͷڞ௨6* w *NBHF$BQUVSF"1* w Χϝϥ΁ͷΞΫηε w 8FC4IBSF"1* w 4/4γΣΞͷڞ௨6*
  6. 18"ؔ࿈πʔϧ w 18"#VJMEFS w .JDSPTPGU੡ w طଘαΠτΛΠϯϓοτʹNBOJGFTU΍TXKTͷ਽ܗΛੜ੒ w 8PSLCPY w

    (PPHMF੡ w $BDIF؅ཧΛ༰қʹ͢ΔϥΠϒϥϦ w ࠾༻ࣄྫΛΑ͘ฉ͘