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

Web to PWA!

Gefy Marcos
November 17, 2018

Web to PWA!

Como transformar um site em uma PWA nota 100 no lighthouse!

Gefy Marcos

November 17, 2018
Tweet

More Decks by Gefy Marcos

Other Decks in Technology

Transcript

  1. "Progressive Web Apps são experiências que combinam o melhor da

    web e o melhor dos aplicativos.” Fonte: Developers Google
  2. O que fazer? • Habilitar HTTPS no servidor, isso é

    obrigatório. • Redirecionar o status 301 também para HTTPS.
  3. Como fazer? • Media queries. • Sempre se preocupar com

    performance e boas práticas. • Lighthouse.
  4. { "name": "Gefy Marcos", "short_name": "Gefy", "theme_color": "#333333", "background_color": "#000000",

    "display": “standalone", "//fullscreen, browser “start_url": "https:"//gefy.com.br/index.html", "orientation": “portrait", "//any, landscape "icons": [{ "src": "/img/logo-512x512.png", "sizes": "512x512", "type": "image/png" }] }
  5. Importante "short_name": "Gefy", • O short_name não pode ter mais

    que 12 letras. • Nome que aparece com o ícone no celular.
  6. Importante • É obrigatório ao menos um ícone 512x512. •

    Icone somente PNG. "icons": [{ "src": "/img/logo-512x512.png", "sizes": "512x512", "type": "image/png" }]
  7. Sempre deve ter o mesmo nome e ficar no mesmo

    local para não gerar duplicação. Importante
  8. Como fazer? if ('serviceWorker' in navigator) { navigator.serviceWorker.register('/sw.js') .then(function(registration) {

    console.log('Registration successful’); }).catch(function(err) { console.log('Registration failed: '); }); }
  9. É ativado uma vez, após o “install”, após o service

    worker assumir o controle dos caches. Activate
  10. self.addEventListener('activate', function(event) { var cacheWhitelist = ['gefy-pwa']; event.waitUntil( caches.keys().then(function(cacheNames) {

    return Promise.all( cacheNames.map(function(cacheName) { if (cacheWhitelist .indexOf(cacheName) ""=== -1) { return caches.delete(cacheName); } }) ); }) ); });
  11. var checkResponse = function(request) { return new Promise(function(fulfill, reject) {

    fetch(request) .then(function(response) { if(response.status ""!== 404) { fulfill(response) } else { reject() } }, reject) }); };
  12. var returnFromCache = function(request) { return caches.open('gefy-pwa') .then(function (cache) {

    return cache.match(request) .then(function (matching) { if(!matching "|| matching.status "== 404) { return cache.match(‘/404.html') } else { return matching } }); }); };
  13. var addToCache = function(request) { return caches.open('gefy-pwa') .then(function (cache) {

    return fetch(request) .then(function (response) { return cache.put(request, response); }); }); };
  14. • O que é e como funciona um PWA. •

    Princípios básicos. • Como testar um PWA. • Cor de tema. • Manifesto. • Service worker. • Cache e redirecionamento. Conclusão