Damilola Ezekiel is a software engineer and a technical writer who enjoys learning new things and sharing them through writing.
Published on
Progressive Web Apps (PWAs) are websites that are built with web technologies and behave like apps. By utilizing the latest technologies, PWAs combine the benefits of web and mobile apps. Many of the features of PWAs make them similar to native applications in terms of user experience.
In this article, we will be building a Progressive Web App with Next.js and then deploying it to a server.
To follow along with this article, here are the things you'll need:
To start a new Next.js project, it is recommended to use create-next-app because it automatically sets up everything. You'll need the run the command below
The above command creates a new Next.js project named demo-app in your computer.
Next, you'll move into the project folder by running this command:
To start your Next.js app in the Development server, run this command:
Now that our Next.js is up and running, we can start building. We will be building a random advice generator app using the Advice Slip JSON API.
This is where all the logic of our app happens. This file contains three functions, the first one is the function that calls the API, the second one is the default function for the page and the last one is a function that helps to get data from the API whenever we call it. Then we return a JSX element that displays the data from the API.
We will also add a little bit of CSS for basic styling. The css code needs to be added to the home.modules.css file, otherwise it won't work.
After running this code, the final result of our Next.js app should look like this.
To turn our Next.js app into a progressive web app, we need to follow the steps below.
The first step is to install the PWA dependency and you can check out the README for the package here:
The next step is to create a manifest.json file, which tells the browser how the Progressive Web App should behave when installed on the user's desktop or mobile device, and there are two ways to do this. You can either create the manifest file manually or use a manifest generator.
We'll be using the simicart PWA manifest generator and you'll need to enter the details of your app to generate the manifest file. The interface should look this:
When you click on the generate manifest button, It downloads the manifest file and you will add the contents of the file to the public folder of your app. You'll also need to rename the manifest.webmanifest file to manifest.json
Your manifest.json file should look similar to this:
In the pages folder, you should create a _document.js file to override the default document in Next.js because pages in Next.js skips the definition of the surrounding document's markup. For example, you never include <html>, <body>, etc.
_document.js forms the overall structure of the HTML. If you need need to modify the html or body tags,you have to do it in the _document.js file.
Lastly, we will need to configure the PWA dependency by adding this piece of code to our next.config.js file. You can check out the PWA plugin for more configuration options.
As you can see in the code above, we disable PWA in development environments since the Cache API/Cache Storage provides an extra layer of caching that can complicate debugging.
After configuring the next.config.js file, we’ll need to run the command below in the terminal. This command builds the application for production usage.
Next we run npm run start to start our Next.js app in production server.
The PWA is now ready and you should see an icon appear in the search bar. By clicking on that icon you will be prompted to install the app
After creating the PWA, you'll notice that some files like service workers and workbox have been added to the public folder. These files are not really important and do not need to be pushed to production. All you need to do is to delete those files and add them to .gitignore
Vercel and Netlify are the most popular hosting and serverless services for Next.js Apps. We will be deploying our app to Vercel, a platform developed by the creators of Next.js.
To deploy your app to Vercel, you'll need to follow these steps:
Check out these links for the live project and Github repo.
Progressive web apps are gaining popularity because of the difficulties faced by users with their native mobile apps. This article is just a basic introduction to PWAs as there are lots of other PWA features that can be implemented in a project to give users a better experience. For further readings, MDN has a lot of resources on Progressive Web Apps.
Get visual proof, steps to reproduce and technical logs with one click
Continue reading
Try Bird on your next bug - you’ll love it
“Game changer”
Julie, Head of QA
Try Bird later, from your desktop