How and Why to Create a Progressive Web App
Tags: pwa, tutorial, webdev
Published: 2022-02-10 14:12:07

It’s no secret that the future of the web is mobile. As such, delivering a seamless user experience for mobile users has become a necessity for almost every software endeavor. 

That being said, creating and maintaining separate mobile applications is not a feasible solution for every dev team. Progressive Web Apps(PWAs) aim to solve this issue by enabling modern web applications to provide an app-like responsive user experience directly through the browser. All these are facilitated without having to create a separate mobile application. 

Progressive Web Apps can also offer offline experiences and can be installed as a desktop or mobile application in any supported operating system.

---

###The Components of a PWA
Ultimately, a PWA is just a regular website with some additional enhancements. There are two specific components required to create a Progressive Web App as the following.

####1. App Manifest
The app manifest is a simple JSON file that contains information about your web app and is queried by the browser. Manifest is used to inform the browser about things like how to display the app, the device orientation, icons to use, and links to the service worker to facilitate functionality like the installation option.

####2. What is a Service Worker?
A service worker is a script that runs in the background separately from your web application. This service worker cannot change any DOM elements. Its sole purpose is to handle background tasks like handling network requests, managing the cache for offline functionality, and sending push notifications.

---

###Creating our PWA

First, we need a website to act as the base for our PWA. The following code block creates a single-page website configuration required to facilitate a PWA.

{% gist https://gist.github.com/LiorB-D/4a9be58f37d1ca3a7ded7a3c84f0aef1 %}

Next, we will create the app manifest file to inform the browser about the application.

{% gist https://gist.github.com/LiorB-D/dabce8d6b8daf6f5f826a8b86f7daed5 %}

After that, we can test if the manifest is properly identified by serving the files through a web server. Navigate to the Application section in the Developer Tools, where you will see the application details defined in the manifest file. Ignore the installability warning there as we have not created the worker yet.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sfywrd3z0ti13n9q34l8.png)

Note that PWA only supports HTTPS websites or applications. Therefore, your server needs to be configured with a valid certificate before creating a PWA.
Now we’ll create a file to act as the service worker.


{% gist https://gist.github.com/LiorB-D/b2a087895a3f12e729844dff065a3977 %}

This file needs to be linked to your HTML file. Earlier, we created a link to a file called `app.js`. Create a file with the same name and add the following code to link the `serviceworker.js` with our website.

{% gist https://gist.github.com/LiorB-D/ad06a25a3a0b97d62f0a82ed198a8267 %}

Next, verify if the service worker is correctly configured by visiting the website and again navigating to the Application section of the Developer Tools. This time there will be no warning indicating a missing service worker. Additionally, you will see an install icon for your application in the browser address bar.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rsk8kztlv8zuoccnqhue.png)

If you click on the install icon, you will be prompted to install the application.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qduqm758d2wc6urm6wkr.png)

After installing the application, it will create a desktop icon and get added to your program's menu. You can directly access the application using this icon or programs menu without visiting the website.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8yuf999m1y93mksv2245.png)

There you have it! We’ve just built our own PWA that you can use on desktop, mobile, and the web.

---

###Conclusion

Using progressive Web Apps is a simple way to provide a near-native mobile or desktop user experience. Since PWAs can be created from any type of application, developers can build universal web-based apps easily and even facilitate offline experiences without the need for platform-specific software.

Ready to deploy your website, but don’t want the bottlenecks? At [Codesphere](https://link.codesphere.com/IK), we’re building the most intuitive cloud provider on the market! Give it a try and let us know what you think!

And as always, happy coding!