README.md

Progressive Web App Demo

A very simple progressive web app demo that will help you get started. Below you can find a quick rundown of what the demo does. The resulting progressive web app can be found here.

index.html

A very straightforward HTML page, stylized using my very own mini.css framework. It contains some simple controls for the user to interact with and some dummy elements to print results and status messages.

One point of interest in this file is the manifest declaration, which looks something like this:

<link rel="manifest" href="./manifest.json">

js/app.js

A simple piece of Javascript code to test the functionality of the progressive web app. We send requests to the JSONPlaceholder API, based on the user's input and retrieve some sample data.

Note that inside the document's loading event, we added the following code:

In this file we define the human-readable name and short_name descriptors for our progressive web app, the icons array for the set of images that will serve as the progressive web app's icon set (we only have one in this demo), the start_url for our application, display mode and the color scheme via background_color and theme_color.

You can find more information on the structure of the progressive web app's manifest here.

service-worker.js

Last, but not least, the service worker is what makes a progressive web app what it is. We first define a name for the cache to use and what resources to be cached, like this:

Then, we deal with the various events. install comes first and it's the event that's fired when you first visit the page. What we want to do in our install event is cache the progressive web app's shell. We achieve this using the following code:

Finally, we deal with the fetch event, which is fired whenever we send a request from our page. What we want to do is check if we have the resource cached and try to serve it from the cache, otherwise we will fetch the resource from the URL, as normal. We also added some error handling at the end to deal with certain requests that returned errors when offline. After all of that, we eneded up with the following code: