denisstasyev / cds2021-workshop-pwa

Progressive Web Apps workshop by Maximiliano Firtman

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Progressive Web Apps Workshop

Chrome Dev Summit 2021

Trainer: Maximiliano Firtman firt.dev @firt

In this hands-on workshop, you will code a Progressive Web App, an app that will be installable on Windows, Mac, Linux, ChromeOS, Android, iOS, and iPadOS with offline capabilities and integration with the device. We will start creating icons and installable metadata for mobile and desktop with the Web App Manifest. After that, we will enhance the user interface with CSS before adding a Service Worker for offline support. Finally, we will work on distribution, generating an "Install" dialog from the browser and PWA Launchers for listing the PWA through the app stores.

Inspired by original repo

How to start?

Follow link on Firt.dev

To run use static server

npx serve ./src

Some notes

Cool notes:

  1. All HTML-pages should include link to Manifest
<link rel="manifest" href="app.webmanifest" />
  1. Due to Web Application Manifest by W3C it's better to use .webmanifest extension over .json for Manifest
  2. scope in Manifest is responsible for folder to be PWA
  3. icons in Manifest should be PNG (SVG supports only in Chrome since summer 2021)
  4. Change Theme Color with JS (works only in PWA-mode)
document.querySelector("meta[name=theme-color").content = "blue";
  1. Theme Color can be adjusted to support light/dark themes with CSS Media (and could be different for different HTML-pages)
<meta name="theme-color" content="#bb7162" media="..." />
  1. Only one Service Worker for Scope in chrome://serviceworker-internals/ possible
  2. After closing tab Service Worker still exists for 40s in Chrome
  3. Service Worker folder is important
  4. Get rid of HTTPS-only for ip:port (instead of localhost:port)
chrome:flags -> "Insecure origins treated as secure"  let's you additional sites where HTTP will work.
  1. Look at Workbox
  2. To publish into any App Stores use PWABuilder. It'll wrap web-app into classic proprietary mobile or desktop app format. But remember about deeplinks (PWABuilder supports them)
  3. To emulate different devices use Xcode (for iOS), Android Studio or Vysor (for Android)

Useful links

  1. chrome://serviceworker-internals/
  2. chrome://inspect/#devices for real Android device Port forwarding

Soon this course will be available at web.dev

Tools

Service Workers Snippets

  1. Registration
if ("serviceWorker" in navigator) {
  navigator.serviceWorker
    .register("serviceworker.js")
    .then(function (registration) {
      // Worker is registered
    })
    .catch(function (error) {
      // There was an error registering the SW
    });
}
  1. Basic Add to Cache
var urls = [];

self.addEventListener("install", (event) => {
  console.log("The SW is now installed");
  event.waitUntil(
    caches.open("myAppCache").then(function (cache) {
      return cache.addAll(urls);
    })
  );
});
  1. Fetch: Cache First
self.addEventListener("fetch", (event) => {
  event.respondWith(
    caches.match(event.request).then(function (response) {
      if (response) {
        // The request is in the cache
        return response;
      } else {
        // We need to go to the network
        return fetch(event.request);
      }
    })
  );
});
  1. Fetch: Network first (server or connection errors)
self.addEventListener("fetch", (event) => {
  event.respondWith(
    fetch(event.request).catch(function (error) {
      return caches.open(myAppCache).then(function (cache) {
        return cache.match(request);
      });
    })
  );
});
  1. Fetch: Stale while Revalidate
self.addEventListener("fetch", (event) => {
  event.respondWith(
    caches.match(event.request).then(function (response) {
      // Even if the response is in the cache, we fetch it
      // and update the cache for future usage
      var fetchPromise = fetch(event.request).then(function (networkResponse) {
        caches.open("activityLauncher").then(function (cache) {
          cache.put(event.request, networkResponse.clone());
          return networkResponse;
        });
      });
      // We use the currently cached version if it's there
      return response || fetchPromise;
    })
  );
});

About

Progressive Web Apps workshop by Maximiliano Firtman


Languages

Language:JavaScript 60.4%Language:CSS 22.9%Language:HTML 16.8%