Progressive web apps (PWA) are becoming an industry standard for building web applications. With reliability, speed and immersive user experience in mind, PWAs help you reach a broader audience. In this session we will take an existing web app and convert it into a PWA. First, we'll make our application installable to homescreen by adding a Web App Manifest json file. Next, we'll focus on making our app reliable by enabling service workers inside our application and caching resources. Throughout this session we'll also learn how to use Lighthouse to test the performance of our PWA.
25. @simona_cotin
–Netlify
“Service workers are proxies
that sit between the web page
and the network, providing
cached versions of the site
when no network connectivity
is available.”
Service Workers 🏋🏋♀️
33. @simona_cotin
// cache a cat SVG
event.waitUntil(
caches.open('static-v1').then(cache => cache.add('/cat.svg'))
);
});
self.addEventListener('activate', event => {
console.log('V1 now ready to handle fetches!');
});
self.addEventListener('fetch', event => {
const url = new URL(event.request.url);
// serve the cat SVG from the cache if the request is
// same-origin and the path is '/dog.svg'
36. @simona_cotin
–Mozilla
“The web app manifest provides
information about an application (such
as name, author, icon, and description)
in a JSON text file. The purpose of the
manifest is to install web
applications to the homescreen
of a device, providing users with
quicker access and a richer
experience.
”
59. @simona_cotin
What if you had an
existing app?
✓ Add service worker package
✓ Enable service worker build support in the cli
✓ Import and register service worker
✓ Create service worker conf file
Hey everyone, this is great! Thank you for being here!
My name is simona and I work as a cloud developer advocate for microsoft and part my role is to make sure that javascript is a first class citizen in the cloud.
And my role is also what makes this talk to exist for very selfish reasons! Just like this meme says, most of a developer advocate’s life is actually spent on a plane
Or on a train on the way to a plane, or at conferences or event super cool hotels that
Have very bad internet! And many times it happens that just before a conference like this one, I have one last thing to add to a demo or slide tweak and ovviously the internet gods are not very good to me
And it kinda feels like this, every single time! So if it were for me, I would make the entire web progressive web apps so that at least sites that I visited in the past will give me content.
Progressive web apps are about much more than just the offline experience. They are about creating a better user experience overall.
And because there are many definitions of PWA let’s start with one of the definitions I find most approachable
And a definition from wikipedia would be:
Or another definition that I really like by Ales Russell,
And some of you might not now, but Alex and his wife, Frances have actually coined the Progressive Web Applications term. While they were out having dinner :) They also came up with the list of things that make up a progressive web app.
https://infrequently.org/2015/06/progressive-apps-escaping-tabs-without-losing-our-soul/
And the fist thing on the list was that these apps should be progressive, meaning that the user experience should enhance as the user uses more the app, they should also work for every user, regardless of browser choice
to fit any form factor, desktop, mobile, tablet, or forms yet to emerge.
Your app should still work offline, or on low quality networks.
Feel like an app to the user with app-style interactions and navigation.
Always up-to-date thanks to the service worker update process.
Served via HTTPS to prevent snooping and ensure content hasn’t been tampered with.
Are identifiable as “applications” thanks to W3C Manifests and Service Worker registration scope allowing search engines to find them
Make re-engagement easy through features like push notifications.
Allow users to “keep” apps they find most useful on their home screen without the hassle of an app store.
Easily shared via a URL and do not require complex installation.
So how do make this happen, in code?
Well, there is this thing called baseline appyness which defines the basic minimal requirements for an app to be a progressive web app. And the first requirement is to implement service workers. The second important thing is to include a web manifest. And all should be served over HTTPS.
So let’s dive into each of these and understand what they are.
https://www.netlify.com/blog/2017/10/31/service-workers-explained/
And if we were to look at a diagram of how this works, we have the web page and the service worker on the same side of the wire, and then over the network we can go retrieve data from the server or we can get that from a cache that sits on the same side as service workers
So in a request-response model, we first make a request from the web page, the request is intercepted by the service worker, which then inspects it, works hard to understand where is should go, and decided to retrieve data from the network
Or it can decide to first retrieve the data from the cache and return that to the user and only after that go over the network and update if necessary.
For a service worker to exist we need to first register it. And we do that by calling the register method on the service worker object available in the browser. But before that we need to check if we have support for service workers.
And a service worker has a scope and a lifecycle that we can tap into
The scope defines which requests can be intercepted by a particular service worker and by default its the current folder and everything bellow. So if we were to register a worker at xyz.sw/work then all requests to those domain or subdomain would be intercepted by our service worker.
A service worker has a lifecycle that is completely separate from your web page.
A service workers life starts after it registers, it gets into the installing stage and from there it can either error or get activated. And then it can move into idle if nothing happens and either terminated of fetch.
If you’re wondering what’s the current support for service workers, its fully supported in Firefox and Chrome, Chrome for android and Samsung internet and it’s in development for edge, safari and safari for iOS.
Service workers enable us to let our users interact with our app even when they’re offline which is a great win.
• Connectivity independent - Service workers allow work offline, or on low quality networks.
• Fresh - Always up-to-date thanks to the service worker update process.
• Discoverable - Are identifiable as “applications” thanks to W3C manifests[6] and service worker registration scope allowing search engines to find them.
• Safe - Served via HTTPS to prevent snooping and ensure content hasn’t been tampered with.
• Installable - Allow users to “keep” apps they find most useful on their home screen without the hassle of an app store.
And this is what makes our basic progressive web app.
So let’s see what happens when we take that to Angular
There are a few options here that can help us get started
And in our app module we import our service worker and then register. And what you don’t see here is that we are only enabling the service worker for production. Can you guess why? Well, as you might’ve imagined, because the service worker caches things you really don’t want in you development process to not have the latest version of your app.
The Angular CLI creates a service worker configuration file, called ngsw-config.json. The configuration file controls how the service worker caches files and data resources.
http://www.pwabuilder.com/
https://github.com/pinterest/service-workers
https://github.com/tastejs/hacker-news-pwas
https://github.com/angular/angular-cli
The Pinterest PWA started because they were focused on international growth, which led them to the mobile web.
https://medium.com/dev-channel/a-pinterest-progressive-web-app-performance-case-study-3bd6ed2e6154
• Reliable - Load instantly and never show the downasaur, even in uncertain network conditions.
• Fast - Respond quickly to user interactions with silky smooth animations and no janky scrolling.
• Engaging - Feel like a natural app on the device, with an immersive user experience.