A gentle Introduction to the concept of Progressive Web Apps, explaining the key concept needed to build a website/app that works even when your user is on a flaky network.
14. “
An app "shell" is the minimal HTML, CSS and
JavaScript required to power the user interface and
when cached offline can ensure instant, reliably
good performance to users on repeat visits
- Google Developers
17. “
A service worker is a script that your browser runs in the
background, separate from a web page, opening the door to
features that don't need a web page or user interaction.
Today, they already include features like push
notifications and background sync.
- Google Developers
18. “
Service workers essentially act as proxy servers that sit
between web applications, and the browser and network
(when available). They are intended to (amongst other
things) enable the creation of effective offline experiences,
intercepting network requests and taking appropriate action
based on whether the network is available and updated
assets reside on the server.
- Mozilla Developer Network
34. “
The web app manifest is a simple JSON file that gives you,
the developer, the ability to control how your app appears to
the user in the areas that they would expect to see apps (for
example the mobile home screen), direct what the user can
launch and more importantly how they can launch it.
- Google Developers
Keep your users in mind and save them from the lie and dinosaur disasters.
Wga
On first load, deliver and cache this basic resources, serve it on next visit instead of fetching the server. Now how do we fetch and cache for next visit?
Wga
SW serves a intermediary between web apps and web servers.
When a service worker is first registered, it fires an "install" event which you can then use to pre-fetch and cache the resources your app needs. SWs goes into the idle mode if no in use and then get called into ‘active’ mode by the browser on need or get terminated when not needed.
it’s important to understand that the FIRST time you load the page, it’s still going to be loading from the network - the index page has already been retrieved from the network, of course, and the page’s resources will kick off downloading normally. The Service Worker will come into play on subsequent loads, not the first load.
Since we've already been to the page, the service worker is already installed and is immediately activated. Then, just like before, the browser checks with the server to see if the service worker has changed and if it does it saves this new resources to the cache.
We are registering SW using this code snippet.
We are registering SW using this code snippet.
There are plenty of ways to do this, but in this case, we iterate over the list of keys from the cache, then delete any caches that don't match the current cache name.
And there's one more thing we need to do. By calling self.clients.claim, we tell the browser that we want THIS service worker to take control of all of the clients that it controls. That means we don't have to wait until any other service workers that are running terminate, we're ready to go immediately!
We’ll assume that every required resource for our app is already in the cache; but if it is not we'll just go out to the network to fetch it. When a network request is made from our web page our Service Worker wakes up and the `fetch` event is triggered.
So what are the ways are we going to use to store our downloaded resources?
Either use cache before network, network before cache or cache then network.
You could also do a generic fallback solution where a request is made first to the cache, then the network, and if both of them fail, a second request is made to the cache for a generic page.
You could also do a generic fallback solution where a request is made first to the cache, then the network, and if both of them fail, a second request is made to the cache for a generic page.
Wga
User gets prompted to add app to home screen after spending some time in the site.
Users can be asked for permission to allow push notification from the site/app .