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

Progressive Web Apps with React

Progressive Web Apps with React

Jonathan Mills

October 10, 2018
Tweet

More Decks by Jonathan Mills

Other Decks in Technology

Transcript

  1. @ j o n a t h a n f

    m i l l s #reactPWA P R O G R E S S I V E W E B A P P S W I T H R E A C T
  2. if('serviceWorker' in navigator) { navigator.serviceWorker.register('/offline.js', { scope: '/' }) .then(function(registration)

    { console.log('Service Worker Registered'); }); navigator.serviceWorker.ready .then(function(registration) { console.log('Service Worker Ready'); }); } SERVICE WORKER
  3. INSTALL const CACHE_NAME = ‘DevUp’ // Version 0.6.5 self.addEventListener('install', e

    => { console.log('installing service worker!!') const timeStamp = Date.now(); e.waitUntil( caches.open(CACHE_NAME).then(cache => { return cache.addAll([ `/`, `/index.html`, `/json/data.json`, `/static/js/bundle.js` ]) .then(() => self.skipWaiting()); }) ); });
  4. INSTALL const CACHE_NAME = ‘DevUp’ // Version 0.6.5 self.addEventListener('install', e

    => { console.log('installing service worker!!') const timeStamp = Date.now(); e.waitUntil( caches.open(CACHE_NAME).then(cache => { return cache.addAll([ `/`, `/index.html`, `/json/data.json`, `/static/js/bundle.js` ]) .then(() => self.skipWaiting()); }) ); });
  5. INSTALL const CACHE_NAME = ‘DevUp' // Version 0.6.5 self.addEventListener('install', e

    => { console.log('installing service worker!!') const timeStamp = Date.now(); e.waitUntil( caches.open(CACHE_NAME).then(cache => { return cache.addAll([ `/`, `/index.html`, `/json/data.json`, `/static/js/bundle.js` ]) .then(() => self.skipWaiting()); }) ); });
  6. FETCH event.respondWith( caches.match(event.request) .then(function(response) { // Cache hit - return

    response if (response) { return response; } var fetchRequest = event.request.clone(); return fetch(fetchRequest).then( function(response) { // Check if we received a valid response if(!response || response.status !== 200 || response.type !== 'basic'){ return response; } var responseToCache = response.clone(); caches.open(CACHE_NAME)
  7. FETCH event.respondWith( caches.match(event.request) .then(function(response) { // Cache hit - return

    response if (response) { return response; } var fetchRequest = event.request.clone(); return fetch(fetchRequest).then( function(response) { // Check if we received a valid response if(!response || response.status !== 200 || response.type !== 'basic'){ return response; } var responseToCache = response.clone(); caches.open(CACHE_NAME)
  8. FETCH event.respondWith( caches.match(event.request) .then(function(response) { // Cache hit - return

    response if (response) { return response; } var fetchRequest = event.request.clone(); return fetch(fetchRequest).then( function(response) { // Check if we received a valid response if(!response || response.status !== 200 || response.type !== 'basic'){ return response; } var responseToCache = response.clone(); caches.open(CACHE_NAME)
  9. event.respondWith( caches.match(event.request) .then(function(response) { // Cache hit - return response

    if (response) { return response; } var fetchRequest = event.request.clone(); return fetch(fetchRequest).then( function(response) { // Check if we received a valid response if(!response || response.status !== 200 || response.type !== 'basic'){ return response; } var responseToCache = response.clone(); caches.open(CACHE_NAME) FETCH
  10. } var fetchRequest = event.request.clone(); return fetch(fetchRequest).then( function(response) { //

    Check if we received a valid response if(!response || response.status !== 200 || response.type !== 'basic'){ return response; } var responseToCache = response.clone(); caches.open(CACHE_NAME) .then(function(cache) { cache.put(event.request, responseToCache); }); return response; } ); }) ); FETCH
  11. } var fetchRequest = event.request.clone(); return fetch(fetchRequest).then( function(response) { //

    Check if we received a valid response if(!response || response.status !== 200 || response.type !== 'basic'){ return response; } var responseToCache = response.clone(); caches.open(CACHE_NAME) .then(function(cache) { cache.put(event.request, responseToCache); }); return response; } ); }) ); FETCH
  12. } var fetchRequest = event.request.clone(); return fetch(fetchRequest).then( function(response) { //

    Check if we received a valid response if(!response || response.status !== 200 || response.type !== 'basic'){ return response; } var responseToCache = response.clone(); caches.open(CACHE_NAME) .then(function(cache) { cache.put(event.request, responseToCache); }); return response; } ); }) ); FETCH
  13. } var fetchRequest = event.request.clone(); return fetch(fetchRequest).then( function(response) { //

    Check if we received a valid response if(!response || response.status !== 200 || response.type !== 'basic'){ return response; } var responseToCache = response.clone(); caches.open(CACHE_NAME) .then(function(cache) { cache.put(event.request, responseToCache); }); return response; } ); }) ); FETCH
  14. MANIFEST { "short_name": "Library", "name": “DevUp Library Demo", "icons": [

    { "src": "book.png", "sizes": "192X192", "type": "image/png" } ], "start_url": "./index.html", "display": "standalone", "theme_color": "#000000", "background_color": "#ffffff" }
  15. PACKAGE.JSON – ADDING GH-PAGES { "name": "library", "version": "0.1.0", "private":

    true, "homepage": “https://jonathanfmills.github.io/DEVUPPWA”, "dependencies": { "gh-pages": "^1.1.0", … }, "scripts": { "predeploy": "npm run build", "deploy": "gh-pages -d build", } }
  16. @ j o n a t h a n f

    m i l l s #reactPWA P R O G R E S S I V E W E B A P P S W I T H R E A C T