# PWA release (optional)

REMARKS

  • PWA and service workers are advanced topics
  • We will not discuss the theory behind these techniques in detail
  • This chapter will only give a brief introduction, and we will try to explain the underlying principles by showing you a practical example of a PWA

# What is a PWA?

  • A Progressive Web App (aka PWA) is a Web App which combines the best features of the Web and Native Apps
  • PWA's cache the content of the website locally, so they are fully accessible even when you lost your internet connection
  • PWA's are also installable on mobile devices and on desktop devices (with Chromium-based browsers)

# PWA vs native apps

FEATURE PWA NATIVE APP
Functions offline
Accessible from the home screen
No download required
Bypass the App/Play store
One codebase
Requires no updates
Push notifications
Device integrated API's limited access (opens new window) full access

# PWA requirements

  • To make the website installable as a PWA, you need:
    • A web manifest file (the realfavicon script already made this for us) with the correct fields (opens new window)
    • The website must be served from a secure (https) connection (http://localhost is also possible)
    • Must have an icon to represent the app on the device (is present in the manifest file)
    • A service worker to allow the app to work offline
  • Only the service worker is missing to transform our "normal" (production release) website into a PWA

# Add To Home Screen (A2HS)

# For mobile

  • Mobile browsers implemented an Add To Home Screen (aka A2HS) feature that takes the information from the app's web manifest file
  • This only works if the app meets all the necessary requirements, as described above
  • The Add to home screen pops up automatically when you stay for at least 30 seconds on the page
    • Open this course website (or scan the QR code (opens new window)) on your mobile device and look what happens after 30 seconds ...

Add to home screen

  • If you canceled the installation, you only get a new pop-up after 90 days!
  • But no worry, you can always install the app from the menu (the dree dots) in the top right corner of your mobile browser

# For Desktop

  • On your Chromium-based desktop browser there is no pop-up to install the app
  • You will find an "install" icon at the right in the address bar
    Install PWA

# What is a service worker?

  • As indicated in the requirements, the only thing missing is a service worker, but what does a service worker do?
  • A service worker is written in JavaScript but works independently of the main browser thread
  • The service worker is a proxy that lives between your website and the operating system
  • The main purposes of the service worker are:
    • synchronize data in the background
    • intercept network requests and receive updates
    • receive push notifications

# How do PWA's work?

# How it works in theory

How PWA's work

  1. The browser checks (in the background) if the service worker has been updated
  2. An update is available?
    • no: stop here and serve the cached version of the website
    • yes: start with serving the cached version of the website but also download the new service worker in the background; after the new service worker is ready for installation, go to step 3
  3. Show a notification inside the browser that there's a new service worker ready to be installed
    • user doesn't accept the notification: the update will be installed after the user closes the browser window and starts it up again
    • user accepts the notification: go to stap 4
  4. The new, updated service worker gets installed and after that the page is reloaded

# Example

  • Let's show this in a real live example and take this course website as a starting point
  • Open this site in another browser (so that you can read the rest of this page)
  • Open the homepage (opens new window) in Chrome
  • Open DevTools in Chrome, and go to the Application tab to remove the already running service worker:
    • Remove the cached files from the Cache Storage
      Clear cache
    • Unregister the service worker from the Service Workers
      Clear cache
  • Reload the page
    • The first time you open a PWA, the service worker is installed automatically and starts caching (downloading) ALL the files from the website and store them in the Cache Storage
  • Go offline, reload the homepage and browse to a different page on the website to see how the caching works
    Go offline
  • Go back online
  • Wait until your teacher changed something on the homepage and pushed it to GitHub
  • Reload the homepage
    • We are now at step 2 in the illustration of the previous section
    • The new service worker (with the orange dot) is available for installation, and the user gets a notification to install it New service worker available
  • Click on Refresh
    • The service worker will be installed (step 3) and the page is reloaded (step 4)
    • All the changed files are cached again in the background
      New service worker installed

# Install the service worker with Gulp

  • We only have to add two extra tasks to the previous build task to install a service worker
  • Our new build script build:pwa (line 1) will look like this:
 


gulp.task('build:pwa', gulp.series('clean', 'copy', 'generate-favicon', 'inject-favicon-markup', , 'concatSW', 'service-worker', 'minify'));
gulp.task('build', gulp.series('clean', 'copy', 'generate-favicon', 'inject-favicon-markup', 'minify'));
1
2

TIPS

  • You can make the service-worker task a lot smaller by pre-caching all the files from your website (like we do in this course)
    • The disadvantage is that it takes up more memory on your (mobile) device





 


// Build service worker logic
gulp.task('service-worker', () => {
    return workboxBuild.generateSW({
        globDirectory: './dist',
        swDest: './dist/sw.js',
        globPatterns: ['**/*.*']
});
1
2
3
4
5
6
7
  • To integrate this service worker into your own project, you only have to copy the two Gulp tasks and the file src/js/serviceWorker/serviceWorker.js

# Hosting

  • Let's update our hosting again
  • Every time (except the first time) you publish a new version on your hosting, you get a conform notification that there is a new version available
  • When you only visited the homepage, the tree images on this page are cached
    Cached images

# FTP hosting

  • Run the build task build:pwa every time want to publish a new version of your PWA
  • Delete all content inside the root folder of your hosting
  • Upload the content of the dist folder (not the content of the src folder!) to the root of your hosting

# Netlify hosting

  • Go to your website settings on Netlify and update the build settings:
    • Replace the Build command with gulp build:pwa or with npm run build:pwa
      Netlify hosting
  • Every time you push a modified version of your website to GitHub, Netlify will automatically pick this up and publish the updated PWA version on your hosting

# Lighthouse report

  • Let's perform a new audit to see how our PWA scores this time
    Lighthouwe PWA score
Last Updated: 4/12/2021, 7:55:49 AM