What if I told you that now a web developer can easily build mobile apps? All you need to know is some HTML, CSS, and JavaScript and you are good to go! No need to go through the pain of learning native languages like Java (for Android Apps) or Objective-C and Swift(for iOS Apps).
It would be even more awesome if you could publish your apps without having to go through the rules and restrictions placed by Play Store and App Store! If you are a Mobile App Developer, you might have experienced the troubles of following the norms of App Store. The App store also has some very strict rules about when you can update your app or what that update can contain (App Store won’t let you publish the update if it changes the fundamental working of your App).
In this post, we will find out about how building Progressive Web Apps lets you do all the things that I mentioned above, and more!
This is a companion post to Neeraj Singh’s talk about Progressive Web Apps, which he gave at ReactFoo Pune 2018.
ReactFoo is a developer conference organized by HasGeek. This conference is full of talks and workshops about the popular web design framework, ReactJS.
The conference was held at MCCIA Trade Tower. There were many amazing talks by well-known developers such as Satyajit Sahoo, Siddharth Kshetrapal, Manjula Dube, Preeti Wadhwani, Jaison John Titus, and many more!
The event was attended by many React developers and enthusiasts who came from places like Ahmedabad, Surat, Hyderabad, Chennai, Mumbai, and of course, Pune!
Preeti, Manjula, and Siddharth leading a Birds of Feather session on testing for React
Simply put, a Progressive Web App is a mixture of a mobile and web app!
But what does this exactly mean? And how are these different from Traditional Web Apps and Native Apps?
Let’s do a brief comparison between Native Apps, Traditional Web Apps, and PWAs
A native mobile app is a smartphone app that is coded in a programming language like Objective-C for iOS or Java for Android operating systems.
Native apps have the following features:
Traditional Web Apps are apps that have the following features:
Along with the features that I have mentioned above, PWAs are also defined by the following features:
According to Comscore Mobile App Report, over 50% of America’s smartphone users download zero apps a month! What this means that the time when our phone used to be filled with apps is slowly fading away!
Statistically, each step to download an app reduces about 20% of the app’s potential number of users.
Progressive web apps have effectively reduced the steps between the discovery of an app and getting it on the home screen, thereby eliminating the friction of getting the app installed on your device.
Another win for PWAs is that native apps are only available via Play Store/App Store. The reason behind the success of these stores is that users have no other choice in how they can find and install native apps.
But when it comes to web apps (both traditional and progressive), there is no single store! So we can have carefully curated app stores that suit our needs perfectly. The opportunities are limitless!
The general process of installing Native Apps is something like this:
Let’s compare that to installing progressive web apps:
Let’s take the example of Flipkart. When you open the Flipkart URL on your mobile’s browser, you will get something like this:
When you open the browser’s menu, you will see the option Add to Home Screen
.
Note: If you get a prompt as shown below, you will know that you are adding a PWA to your home screen.
But if the browser instead asks you to enter a name, then it is not a PWA.
Once your PWA is installed, it will appear on your home screen like this:
Just like a Native App, your PWA will also be available in the device’s app drawer.
Now when you open the PWA, you will notice that is exactly like what you saw on your web browser. The only difference is that in the PWA, there is no URL bar.
Chrome browser will automatically send you a prompt to add the PWA to your home screen if you have opened the webpage 2X times for two weeks.
Manifest is a simple JSON
file that gives the developer the ability to control how the app should appear to the user in areas where they would expect to see apps (a mobile device’s home screen), direct what the user can launch, and define its appearance at launch.
Here’s an example for manifest.json
file of a PWA app:
To link this JSON
file to your PWA, use:
<link rel="manifest" href="/manifest.json">
A push notification is a message that pops up on the user’s device. Push Notifications can be triggered locally by an open application, or they can be “pushed” from the server to the user even when the app is not running. Push notifications allow users to opt-in to timely updates and also allow you to effectively re-engage with customized content.
Push Notifications are assembled using two APIs: the Notifications API and the Push API. The Notifications API lets the app display system notifications to the user. The Push API allows a service worker to handle Push Messages from a server, even while the app is not active.
The Notification and Push API’s are built on top of the Service Worker API, which responds to push message events in the background and relays them to your application.
I have been mentioning service workers multiple times in this post. But what are these Service Workers?
Service Worker is a script that your browser runs in the background, separate from the web page/web app. It is generally used for features that don’t need a web page or user interaction.
Service Workers provide the technical foundation that features like offline experiences, periodic background syncs, and push notifications rely on.
To install a service worker you need to kick-start the process by registering it, so your app knows where the service worker actually lives.
To know more about Service Workers:
Service Workers: an Introduction | Web Fundamentals | Google Developers_Rich offline experiences, periodic background syncs, push notifications—functionality that would normally…_developers.google.com
To implement push notifications, you need to configure both the Client Side and the Server Side of your PWA.
Let’s start with the Server Side first.
At the server side of your PWA,
There are multiple ways through with a server can send Push Notifications to the user. One such method is the VAPID(Voluntary Application Server Identification for Web Push) Protocol. VAPID uses JSON Web Tokens to carry information.
The core of VAPID is called claim. A claim is a JSON object that contains several common fields.
The application server creates a public/private key pair. The public key is given to your web app, whereas the private key stays with the admin.
When the user elects to receive push notifications, the PWA adds the public key to the subscribe()
call’s options
object.
Now the app’s server sends a push message, it includes a signed JSON web token along with the public key.
If this public key matches the one that user has, the data is decrypted.
This method prevents unauthorized push services, which otherwise would have lead to a greater risk of DOS attack. Also, any app server in possession of the endpoint can send messages to your user, and there is no way for the push service to contact the developer if there are problems.
Web push notifications are messages that come from the web. You get them on your device when the PWA is not open and is usually used to re-engage the users without having to know their details.
You can set up Web Push by using Google Cloud Messaging service or by using VAPID as shown below:
Now you can send Push Notifications to your device! Here’s how you can configure your push notification:
At the client side of your PWA,
Here’s an Even Listener that will “listen” to when you receive a Push Notification:
Internet connections can often become flaky or even non-existent when you are on the go. This is why PWA comes with offline capability, which allows it to work even when there is no internet connectivity at all!
In a PWA, the service worker acts as a network proxy. So when a user opens a new screen of the PWA, the request for that page first goes to the service worker. The Service Worker then tries to access that content from the cache.
If the content is available from the service worker, then we will get the new page, regardless of the network connectivity. Otherwise, the service worker redirects the request to the network.
It is up to the developer to release updates for the PWA. A developer can release the update whenever he/she wants. PWAs don’t have to bother with the play store restrictions.
The user can decide whether they want to update their app. Since the service worker first sends the requests to the cache, the working of the app is not affected by the new updates that are available. The cache is only updated when the app is closed.
The developer needs to make sure that the new update does not interfere with the user interactivity. So it is advised that the developer should just send a prompt to the user to update the app, and the user can update it whenever he is free.
If you have ever used Whatsapp Web, you might have seen the occasional push notification informing you of a new update.
One might wonder just how compatible are PWAs with existing architectures using libraries like React. Does a PWA require a wholesale rewrite?
There are many advantages if you use React to build your PWA. One these advantages is better discoverability.
If you develop a Single Page App (SPA), you will have the client-side JavaScript which will execute in the browser and Google would then be able to index it.
React provides the method React.renderToString()
, which will render a React element to its initial HTML. You can use this method to generate HTML on the server and send the markup down on the initial request for faster page loads and to allow search engines to crawl your pages for SEO purposes.
Another advantage of using React for PWA is Async Import.
It often so happens that a user does not use a certain part of the app. But our app has to bundle all the components and their dependencies into the bundle.js file. By doing this, not only does the size of app increase, but the app also takes longer to load.
We can instead use Async Import to define Higher Order Component (HOC) that will load different parts of your app only when the user needs them.
By implementing the above code, your app will load the LineChart
file only when the user requests.
To know more, check out this post by Anton Lavrenov
Progressive loading for modern web applications via code splitting_Are your users tired of waiting when your app is loading and they close the tab? Let’s fix it with the progressive…_blog.lavrton.com
Google has provided us with two useful apps called Lighthouse and Workbox to help us build better PWAs.
Lighthouse is a tool which is very useful for auditing an app for PWA features and checking whether your app meets a respectable bar for web performance under emulated mobile conditions.
Lighthouse is available as a Chrome extension and a CLI, both of which provides a report that looks something like this:
The top-level audits Lighthouse runs effectively a collection of modern web best practices refined for a mobile world:
Here’s a getting started guide for Lighthouse.
Lighthouse | Tools for Web Developers | Google Developers_Learn how to set up Lighthouse to audit your web apps._developers.google.com
You can also check out this post by Addy Osmani to know more,
Progressive Web Apps with React.js: Part I — Introduction_Progressive Web Apps take advantage of new technologies to bring the best of mobile sites & native apps to users…_medium.com
Workbox is the successor to [sw-precache](https://github.com/GoogleChrome/sw-precache)
and [sw-toolbox](https://github.com/GoogleChrome/sw-toolbox)
. It is a collection of libraries and tools used for generating a service worker, routing, and runtime-caching. Workbox also includes modules for easy integration of background sync and Google analytics into your service worker.
To generate a precache:
Workbox also provides you with various caching strategies such as:
Check out this CodeLab to learn how to build a PWA using Workbox:
Build a PWA using Workboxcodelabs.developers.google.com
PWA is the next step in web development. So try it out and be ready for the Future!
I am Rajat S, a Technical Content Writer at GeekyAnts. An aspiring coder who has a long way to go. A Die-Hard DC Comics Fan who loves Marvel Movies. 😛 Follow me on Twitter to know more about all the amazing things that are happening at GeekyAnts.
Thanks to Neeraj Singh, who is a Senior Software Engineer at GeekyAnts. Neeraj was at ReactFoo Pune to talk about Progressive Web Apps.
And thank you for reading! Please do 👏 if you liked it.