โจ How to make a Next.js app a PWA with offline support

I am a full stack developer, YouTuber and blogger!
Search for a command to run...

I am a full stack developer, YouTuber and blogger!
Nice one Usman!
Digging the offline capabilities, really need to get into those, and this article will surely help โจ
Thank you Chris! next-pwa makes the offline support very easy with no configuration
Hey all ๐๐ Webhooks are a powerful tool that allow applications to communicate with each other in real-time. They are often used to send notifications or updates from one system to another without requiring manual intervention. If you know the bas...

Hey everyone! It's been a while since I wrote my last article. But here I am with another topic that might be informational to you ๐. This time I am writing about why you should avoid the onclick attribute in your HTML and JavaScript code. This ar...

โ What and Why Memoriez โ This is a very important question because it makes things clear as to why this app is needed. I built Memoriez because I honestly found it hard to write and maintain journal/diary entries daily. It's not easy to maintain a j...

Explained with a simple project

๐ Hey developers! This post is about how you can sort import statements in your JavaScript/TypeScript/React/Node etc projects easily with Prettier in VS Code when you format the code. https://www.youtube.com/watch?v=QQWgN0_gUxI What will you achieve...

In today's article, we will see how we can make a PWA out of a Next.js app! And it is going to be very exciting as the biggest feature will be offline support with a very little configuration!
next-pwa packageBefore we move on, I would like to recommend checking my video on YouTube about this if you are comfortable following along with a video. Make sure to leave a comment about any suggestions, or doubts you have ๐.

For this demo, I already have a simple project setup that we can use to start or test.
You can download the source or clone the repo: GitHub Link

This repository has 2 branches, the
mainone contains the starter code and thepwabranch contains the complete code.
next-pwa packageThe next step is to install the NPM package we need for this. It's a great package that does everything for you automatically.
npm i next-pwa
# or yarn add next-pwa
If you are familiar with making a PWA, you already know that we need different sizes of logos for our app to function properly on different devices. I found a very good website to generate these images in different sizes.
You can simply go to Icongen and select the types you want. For the sake of this tutorial, I am generating these images. And upload vercel.svg or your existing logo.

After getting all the images, add them to a new folder named icons in the project's public folder.
Rename all the file names with icon-{size}x{size}.png. For eg, icon-192x192.png

Additionally, add a maskable.png which you can either generate online or for the sake of this tutorial, I used the same 192x192 sized icon for the maskable icon.
Now we need to create a file for the metadata about the app. Create a file named manifest.json in the public folder and copy the contents of the file from here.
You can fill up the contents as per your project, for this project, this is the data we are using
{
"short_name": "Next PWA",
"name": "Next PWA Tutorial",
"description": "...",
"icons": [
{
"src": "/icons/icon-36x36.png",
"type": "image/png",
"sizes": "36x36"
},
{
"src": "/icons/icon-48x48.png",
"type": "image/png",
"sizes": "48x48"
},
{
"src": "/icons/icon-72x72.png",
"type": "image/png",
"sizes": "72x72"
},
{
"src": "/icons/icon-96x96.png",
"type": "image/png",
"sizes": "96x96"
},
{
"src": "/icons/icon-144x144.png",
"type": "image/png",
"sizes": "144x144"
},
{
"src": "/icons/icon-192x192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "/icons/icon-512x512.png",
"type": "image/png",
"sizes": "512x512"
},
{
"src": "/icons/maskable.png",
"type": "image/png",
"sizes": "192x192",
"purpose": "maskable"
}
],
"start_url": "/",
"background_color": "#FFFFFF",
"display": "standalone",
"scope": "/",
"theme_color": "#000000"
}
Now you need to link the manifest.json file in your Next.js <Head> component in all the pages.
<Head>
<link rel='manifest' href='/manifest.json' />
</Head>
In the first step, we installed the next-pwa NPM package which we will be using now. In your project's root, create a next.config.js or edit if it already exists.
We import the withPWA function from the package and wrap the export in that function, here we can add a pwa object which we will configure like so ๐
The dest property is the destination folder and we added a disable property which disables creating the service workers while we are in development.
const withPWA = require('next-pwa');
module.exports = withPWA({
pwa: {
dest: 'public',
disable: process.env.NODE_ENV === 'development',
},
reactStrictMode: true,
});
Additionally, we can also add a page to show if the user is offline, this enhances the user experience and makes it work like an actual app.
Just add a page named _offline.js in your pages folder, and next-pwa will automatically show that page if the user is offline. But the condition is that the user has to visit the website so that the offline page is cached whenever the user first visits the website.
Here are some examples of offline pages:

๐ Easy Pastes

๐ YouTube

๐ Google Meet
A final touch would be to add these files to your .gitignore file to not commit these service worker files created by next-pwa
# PWA files
**/public/precache.*.*.js
**/public/sw.js
**/public/workbox-*.js
**/public/worker-*.js
**/public/fallback-*.js
**/public/precache.*.*.js.map
**/public/sw.js.map
**/public/workbox-*.js.map
**/public/worker-*.js.map
**/public/fallback-*.js
And we successfully completed the process, and now your Next.js app can be used offline, is installable as an app, and is also a PWA.

![]()
I hope it helped you make your app a PWA, let me know your experience in the comments and share it with people so that they can do it easily too.