How to Create a PWA With Next.js

Hey all,

This post is in collaboration with James Q Quick. If you like to see videos then check out this video by James Q Quick.

So let’s get the party started.

What is a PWA?

PWA stands for Progressive web app. It is basically like a…


This content originally appeared on DEV Community and was authored by Avneesh Agarwal

Hey all,

This post is in collaboration with James Q Quick. If you like to see videos then check out this video by James Q Quick.

So let's get the party started.

What is a PWA?

PWA stands for Progressive web app. It is basically like a web app built with HTML, CSS, and Javascript with a few more features like-

  • Extremely Fast
  • Installable
  • Works in all browsers
  • Provides an offline page
  • Push notifications

Setup

Creating a Next.js app

npx create-next-app next-pwa-demo

I am going to convert the default Next.js template into a PWA, you can convert your web app.

Installing the required dependency

npm i next-pwa # npm
yarn add next-pwa # yarn

Generating a manifest

I am going to use Simicart for generating the manifest. You can simply add the details of your app and it will generate a manifest. Make sure to select standalone in the display

image.png

The generated manifest will look similar to this

{
  "theme_color": "#000000",
  "background_color": "#ffffff",
  "display": "standalone",
  "scope": "/",
  "start_url": "/",
  "name": "Next PWA",
  "short_name": "PWA",
  "description": "This app is to demo PWA in Next.js",
  "icons": [
    {
      "src": "/icon-192x192.png",
      "sizes": "192x192",
      "type": "image/png"
    },
    {
      "src": "/icon-256x256.png",
      "sizes": "256x256",
      "type": "image/png"
    },
    {
      "src": "/icon-384x384.png",
      "sizes": "384x384",
      "type": "image/png"
    },
    {
      "src": "/icon-512x512.png",
      "sizes": "512x512",
      "type": "image/png"
    }
  ]
}

After you are done with adding all the details, install the zip file and extract it. After you have extracted, drag and drop all the files in the public folder.
We will rename manifest.webmanifest to manifest.json.

Finally, the public directory should look like this

image.png

Creating _document.js

Create _document.js in the pages folder and add the following piece of code

import Document, { Html, Head, Main, NextScript } from "next/document";

class MyDocument extends Document {
  render() {
    return (
      <Html>
        <Head>
          <link rel="manifest" href="/manifest.json" />
          <link rel="apple-touch-icon" href="/icon.png"></link>
          <meta name="theme-color" content="#fff" />
        </Head>
        <body>
          <Main />
          <NextScript />
        </body>
      </Html>
    );
  }
}

export default MyDocument;

Configuring PWA in Next config

We will need to add some data for configuring the PWA, so add this snippet in next.config.js.

const withPWA = require("next-pwa");

module.exports = withPWA({
  pwa: {
    dest: "public",
    register: true,
    skipWaiting: true,
  },
});

You have made your app a PWA 🥳

image.png

Making better development environment

Adding the auto-generated files to .gitignore

If you notice then a few files like service workers, and workbox has been added to the public folder.

image.png

These files change constantly and are not needed in your GitHub. So, do the following to remove them from production.

  • Delete sw.js, sw.js.map, workbox-****.js and workbox-****.js.map.

  • Add the files to .gitignore

# PWA files
**/public/sw.js
**/public/workbox-*.js
**/public/worker-*.js
**/public/sw.js.map
**/public/workbox-*.js.map
**/public/worker-*.js.map
  • Now if you restart the server then the filenames will be dark

image.png

Disabling PWA in development

In development, you might want to disable PWA because it gives a lot of console messages.

We will disable it like this in next.config.js

const withPWA = require("next-pwa");

module.exports = withPWA({
  pwa: {
    dest: "public",
    register: true,
    skipWaiting: true,
    disable: process.env.NODE_ENV === "development",
  },
});

Hope you found this useful. See ya in the next one ✌🏻

Useful links-

James's video

Follow James on Twitter

All socials


This content originally appeared on DEV Community and was authored by Avneesh Agarwal


Print Share Comment Cite Upload Translate Updates
APA

Avneesh Agarwal | Sciencx (2021-10-05T16:39:50+00:00) How to Create a PWA With Next.js. Retrieved from https://www.scien.cx/2021/10/05/how-to-create-a-pwa-with-next-js/

MLA
" » How to Create a PWA With Next.js." Avneesh Agarwal | Sciencx - Tuesday October 5, 2021, https://www.scien.cx/2021/10/05/how-to-create-a-pwa-with-next-js/
HARVARD
Avneesh Agarwal | Sciencx Tuesday October 5, 2021 » How to Create a PWA With Next.js., viewed ,<https://www.scien.cx/2021/10/05/how-to-create-a-pwa-with-next-js/>
VANCOUVER
Avneesh Agarwal | Sciencx - » How to Create a PWA With Next.js. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/05/how-to-create-a-pwa-with-next-js/
CHICAGO
" » How to Create a PWA With Next.js." Avneesh Agarwal | Sciencx - Accessed . https://www.scien.cx/2021/10/05/how-to-create-a-pwa-with-next-js/
IEEE
" » How to Create a PWA With Next.js." Avneesh Agarwal | Sciencx [Online]. Available: https://www.scien.cx/2021/10/05/how-to-create-a-pwa-with-next-js/. [Accessed: ]
rf:citation
» How to Create a PWA With Next.js | Avneesh Agarwal | Sciencx | https://www.scien.cx/2021/10/05/how-to-create-a-pwa-with-next-js/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.