Blogs

Tools

Quick Links

Mridul.tech

How to Build PWA with Next JS?

Mridul Panda

Mridul Panda

Aug 19, 2023

·

5 Min Read

How to Build PWA with Next JS?
How to Build PWA with Next JS

Progressive Web Apps (PWAs) have taken the web development world by storm, providing users with fast, reliable, and engaging web experiences. Among the various frameworks available for building PWAs, Next JS stands out as a popular choice due to its simplicity and flexibility. In this guide, we will explore the process of building a PWA with Next JS, step by step. Whether you’re a seasoned developer or just starting, this article will provide you with valuable insights into creating a high-performance PWA.

How to Build PWA with Next JS

Progressive Web Apps (PWAs) are web applications that combine the best of both web and mobile apps. They offer a responsive, app-like experience while maintaining the flexibility of the web. PWAs are known for their speed, offline capabilities, and seamless user interactions. One of the most efficient frameworks for building PWAs is Next JS. It simplifies the development process and enhances the performance of web applications.

In this guide, we will walk you through the process of building a PWA with Next JS. Whether you’re a developer looking to expand your skills or a business owner aiming to provide an exceptional user experience, this guide has you covered.

Understanding Progressive Web Apps (PWAs)

Before diving into the technical aspects, let’s get a clear understanding of what PWAs are and why they matter. PWAs are web applications that provide an app-like experience to users, including offline access, push notifications, and fast loading times. They are designed to work on any device and are easily installable without the need for an app store.

Setting Up Your Development Environment

To build a PWA with Next JS, you need to have the right development environment in place. Here are the steps to get started:

1. Install Node.js: Ensure you have Node.js installed on your computer. You can download it from the official website.

2. Create a New Next JS Project: Use the Next.js CLI to create a new project. Run the following command:

npx create-next-app pwa-nextjs

Replace “my-pwa” with your project name.

3. Navigate to the Project Directory: Move into your project directory using the command:

cd my-pwa

4. Install the Necessary packages: To create PWA in Next JS we need to Install a package named next-pwa

npm i next-pwa

5. Configuration for PWA: Create a next.config.js file in your project’s root directory if you don’t already have one. Then, add the following configuration to enable next-pwa:

const withPWA = require("next-pwa")({
  dest: "public",
  register: true,
  skipWaiting: true,
  disableDevLogs: true,
});

module.exports = withPWA({
  // rest of next.js config
})

If you are using @next/bundle-analyzer then you can configure your next.config.js like this

const withPWA = require("next-pwa")({
  dest: "public",
  register: true,
  skipWaiting: true,
  disableDevLogs: true,
});

const withBundleAnalyzer = require("@next/bundle-analyzer")({
  enabled: process.env.ANALYZE === "true",
});

const nextConfig = withBundleAnalyzer(
  withPWA({
    // rest of next.js config
}))

Also Read: Next JS Project Ideas to Boost Your Portfolio

6. Host Static Files Copy files to your static file hosting server, so that they are accessible from the following paths: https://yourdomain.com/sw.js and https://yourdomain.com/workbox-*.js. If you are using Vercel to host your site then it will handle all the PWA static files. For the you can check How to Deploy Next JS on Vercel

7. Add Manifest File: For PWA you need to add a manifest.json file. To create one go to Web App Manifest Generator and fill up your app details. It will give you the manifest.json file content. The Manifest file content will look like this

Tip: To create manifest.json go to Web App Manifest Generator

{
  "short_name": "next-js-pwa",
  "name": "Next JS PWA",
  "start_url": "/",
  "display": "standalone",
  "scope": "/",
  "theme_color": "#35155D",
  "background_color": "#ffffff",
  "icons": [
    {
      "src": "/icons-192.png",
      "type": "image/png",
      "sizes": "192x192"
    },
    {
      "src": "/icons-256.png",
      "type": "image/png",
      "sizes": "256x256"
    },
    {
      "src": "/icons-512.png",
      "type": "image/png",
      "sizes": "512x512"
    }
  ]
}

Now you have to link the manifest.json in the head. For that go to _document.js file and add the link.

import { Html, Head, Main, NextScript } from 'next/document'

export default function Document() {
  return (
    <Html>
      <Head>
        <link rel="icon" href="/favicon.ico" />
        <link rel="manifest" href="/manifest.json" />
      </Head>
      <body>
        <Main />
        <NextScript />
      </body>
    </Html>
  )
}

8. Add PWA Icons: In the previous steps we have added the app logo links, so we have to provide the logo in the specific sizes in public folder.

Now start your next js server with npm run dev command and you will notice a download icon on the right side of URL bar. Click on the download icon to download the PWA. Thats it.

The next-pwa package has so many options. You can check them out here.

Even more on the PWA Next JS

With next-pwa configured, your Next.js application is now a PWA. You can enhance it further by:

One of the core principles of PWAs is delivering a fast and responsive experience to users. To achieve this, you can implement several optimization techniques:

1. Code Splitting: Next.js already offers code splitting out of the box, allowing you to load only the JavaScript needed for a particular page. This reduces initial loading times and enhances performance.

2. Lazy Loading: Lazy loading involves loading assets (such as images) only when they are needed, rather than all at once. This can significantly improve page load times, especially for pages with extensive media content.

3. Image and Asset Optimization: Compress and optimize your images and assets to reduce their file sizes. Tools like ImageMagick, TinyPNG, or image optimization plugins for your build tools can help with this.

Testing and Deployment of PWA Next JS

Before deploying your PWA, thoroughly test it on various browsers and devices to ensure a seamless user experience. Once you’re satisfied with the results, choose a hosting platform (e.g., Vercel, Netlify) and deploy your PWA. Configure DNS settings to point to your domain.

Also Read: How to create a URL Shortener with Next JS and Sanity

Conclusion

Building a PWA with Next.js using the next-pwa package is an excellent choice for enhancing your web application’s performance and user experience. Follow the steps outlined in this guide to create a PWA that loads quickly, works offline, and engages users effectively.

Now that you have the knowledge and tools, start building your Progressive Web App and stay ahead in the world of modern web development.

Tags :#pwa

You may also like

How to add Google Web Stories in Next JS

How to add Google Web Stories in Next JS

Dec 14, 2023

·

10 Min Read

In the fast-paced digital world, user engagement is key to the success of any website. One effective way to captivate your audience is by incorporating Google Web Stories into your Next JS website. These visually appealing and interactive stories can make your content more engaging and shareable. In this comprehensive guide, we’ll walk you through […]

Read More

How to send Emails in Next JS for Free using Resend

How to send Emails in Next JS for Free using Resend

Nov 10, 2023

·

7 Min Read

Sending emails in web applications is a crucial feature, and in this article, we will explore how to send Emails in Next JS for free using Resend. Next JS is a popular framework for building React applications, and Resend is a handy tool for email integration. By the end of this guide, you’ll have the […]

Read More

How to add Google Login in Next.js with Appwrite

How to add Google Login in Next.js with Appwrite

Nov 01, 2023

·

7 Min Read

Are you looking to enhance user authentication in your Next.js application? Integrating Social Login with Appwrite can be a game-changer. Add Google Login to your Next.js app with Appwrite. This article will guide you through the process, and practical tips to add Google Login in Next.js with Appwrite. GitHub Code: Google Login in Next.js with […]

Read More

JavaScript Project Ideas to Boost Your Portfolio

JavaScript Project Ideas to Boost Your Portfolio

Oct 13, 2023

·

3 Min Read

JavaScript is the backbone of web development, and mastering it is essential for any aspiring developer. While learning the basics is crucial, building real-world projects is the key to solidifying your knowledge. In this comprehensive guide, we’ll present a diverse range of JavaScript project ideas that cater to different skill levels and interests. These projects […]

Read More

How to Generate robots.txt in Next JS?

How to Generate robots.txt in Next JS?

Oct 05, 2023

·

4 Min Read

In the world of web development and search engine optimization (SEO), ensuring that your website is easily accessible and properly indexed by search engines is paramount. One essential tool that aids in this process is the creation of a robots.txt file. In this comprehensive guide, we, the experts, will walk you through the process of […]

Read More

How to Generate Sitemap in Next JS?

How to Generate Sitemap in Next JS?

Sep 28, 2023

·

8 Min Read

In today’s digital landscape, optimizing your website’s SEO is crucial to attracting more organic traffic and ranking higher on search engine result pages (SERPs). One essential aspect of SEO is creating a sitemap, which helps search engines crawl and index your website efficiently. If you’re using Next JS for your website development, this guide will […]

Read More

Do you want more articles on React, Next.js, Tailwind CSS, and JavaScript?

Subscribe to my newsletter to receive articles straight in your inbox.

If you like my work and want to support me, consider buying me a coffee.

Buy Me A Coffee

Contact Me ☎️

Discuss A Project Or Just Want To Say Hi?
My Inbox Is Open For All.

Mail : contact@mridul.tech

Connect with me on Social Media

Contact Art