Building a Progressive Web App (PWA) with Next.js – Installable, Offline-Ready, and Push-Enabled

With Next.js, you can build a Progressive Web App that behaves like a native mobile app — installable, fast, offline-capable, and able to send push notifications. This article walks you through each step: creating a manifest, setting up push notifications, registering a service worker, securing your app, and testing locally.

PWAPush NotificationsService WorkerWeb App Manifest

~2 دقیقه مطالعه · آخرین به‌روزرسانی ۲۲ آذر ۱۴۰۴

What is a PWA?


A Progressive Web App combines the best of web and mobile:

  • Installable: Add to home screen like a native app
  • Push Notifications: Engage users even when the site is closed
  • Offline Support: Works without internet
  • Fast & Responsive: Smooth experience even on slow networks
  • No App Store: Instant updates, no approval needed

Step 1: Create Web App Manifest


Create app/manifest.ts with metadata and icons:

export default function manifest() {
  return {
    name: 'Next.js PWA',
    short_name: 'NextPWA',
    description: 'A Progressive Web App built with Next.js',
    start_url: '/',
    display: 'standalone',
    background_color: '#ffffff',
    theme_color: '#000000',
    icons: [
      { src: '/icon-192x192.png', sizes: '192x192', type: 'image/png' },
      { src: '/icon-512x512.png', sizes: '512x512', type: 'image/png' },
    ],
  }
}

Place icons in the public/ folder.


Step 2: Add Push Notifications


In app/page.tsx, create a client component that:

  • Registers a service worker
  • Subscribes/unsubscribes the user
  • Sends test notifications

Step 3: iOS Install Prompt


Detect iOS and standalone mode to show install instructions only when needed.


Step 4: Server Actions


In app/actions.ts, implement:

  • subscribeUser: Save subscription
  • unsubscribeUser: Remove subscription
  • sendNotification: Send push using web-push

Step 5: Generate VAPID Keys


npm install -g web-push
web-push generate-vapid-keys

Add keys to .env.local:

NEXT_PUBLIC_VAPID_PUBLIC_KEY=...
VAPID_PRIVATE_KEY=...

Step 6: Create Service Worker


In public/sw.js, add listeners for:

  • push: Show notification
  • notificationclick: Open site

Step 7: Security Headers


In next.config.js, add headers for:

  • X-Content-Type-Options
  • X-Frame-Options
  • Referrer-Policy
  • Content-Security-Policy for /sw.js

Step 8: Test Locally


next dev --experimental-https

Verify:

  • HTTPS is enabled
  • Notifications are allowed
  • Service Worker is registered
  • Manifest is valid (DevTools → Application)

Optional: Offline Support with Serwist


npm install serwist

See Serwist + Next.js example for full offline support.


نوشته و پژوهش‌شده توسط دکتر شاهین صیامی

مقالات مرتبط

Advanced Client-Side Routing and Performance Hooks in Next.js

Next.js provides a rich set of client-side hooks and caching utilities that empower developers to build dynamic, responsive, and secure applications. From reading route parameters to tracking navigation state and reporting performance metrics, this guide walks you through the most important tools available in the App Router.

ادامه

Handling Authorization and Caching in Next.js: A Developer’s Guide

Next.js introduces powerful experimental features for access control and smart caching. This guide covers the unauthorized() function for custom 401 handling, unstable_cache for persistent memoization, updateTag for instant cache invalidation, and useLinkStatus for inline navigation feedback. Learn how to use these tools to build secure, performant, and responsive applications.

ادامه

redirect and refresh in Next.js — Smart Redirects and Client Refreshing via Server Actions

The redirect function in Next.js allows you to navigate users to a new route, returning either a 307 or 303 HTTP response depending on context. It works in Server Components, Client Components, Route Handlers, and Server Actions. The refresh function is used exclusively within Server Actions to refresh the client router. This article explains how both functions work, with practical examples and key considerations.

ادامه

NextRequest and NextResponse in Next.js — Managing Cookies, Headers, Redirects, and Rewrites

Next.js extends the native Web Request and Response APIs with NextRequest and NextResponse, offering powerful tools for managing cookies, headers, redirects, rewrites, and JSON responses. These utilities simplify server-side logic and improve control over routing, personalization, and security. This guide walks through their capabilities with practical examples and best practices.

ادامه

headers, ImageResponse, notFound, and permanentRedirect in Next.js — Request Handling, Dynamic Images, Errors, and Redirects

Next.js offers powerful tools for handling HTTP requests and responses in Server Components. The headers function lets you read incoming request headers. ImageResponse allows you to generate dynamic images using JSX and CSS. The notFound function renders a custom 404 page, and permanentRedirect enables permanent redirection to another route. This article explains how to use each feature with practical examples.

ادامه

A Complete Guide to Using metadata and generateMetadata in Next.js

In modern versions of Next.js, managing page metadata is more powerful and intuitive than ever. Metadata is automatically injected into the <head> of your pages and plays a vital role in SEO, social sharing, and user experience. This guide explains the two main ways to define metadata: using the static metadata object and the dynamic generateMetadata function.

ادامه