The manifest: built in
With the App Router, add app/manifest.ts and Next.js serves it and links it for you:
// app/manifest.ts
import type { MetadataRoute } from 'next';
export default function manifest(): MetadataRoute.Manifest {
return {
id: '/',
name: 'My App',
short_name: 'MyApp',
start_url: '/?source=pwa',
display: 'standalone',
background_color: '#F4F6FB',
theme_color: '#5A0FC8',
icons: [
{ src: '/icon-192.png', sizes: '192x192', type: 'image/png' },
{ src: '/icon-512.png', sizes: '512x512', type: 'image/png' },
{ src: '/icon-maskable-512.png', sizes: '512x512', type: 'image/png', purpose: 'maskable' },
],
};
}
Option 1: a small hand-written worker
If you only need installation and push (no offline caching), put a sw.js in public/ and register it from a client component. The official Next.js documentation has a PWA guide that follows this route, including web push with server actions.
Option 2: offline caching with Serwist
Serwist is a maintained fork of Workbox with a first-party Next.js integration. It is the recommended replacement for the older next-pwa package.
npm i @serwist/next && npm i -D serwist
// next.config.mjs
import withSerwistInit from '@serwist/next';
const withSerwist = withSerwistInit({ swSrc: 'app/sw.ts', swDest: 'public/sw.js' });
export default withSerwist({ /* your Next.js config */ });
// app/sw.ts
import { defaultCache } from '@serwist/next/worker';
import type { PrecacheEntry, SerwistGlobalConfig } from 'serwist';
import { Serwist } from 'serwist';
declare global {
interface WorkerGlobalScope extends SerwistGlobalConfig {
__SW_MANIFEST: (PrecacheEntry | string)[] | undefined;
}
}
declare const self: ServiceWorkerGlobalScope;
const serwist = new Serwist({
precacheEntries: self.__SW_MANIFEST,
skipWaiting: false,
clientsClaim: true,
navigationPreload: true,
runtimeCaching: defaultCache,
fallbacks: { entries: [{ url: '/~offline', matcher: ({ request }) => request.destination === 'document' }] },
});
serwist.addEventListeners();
Add public/sw* to .gitignore, create an app/~offline/page.tsx, and add "webworker" to lib in tsconfig.json.
Things to watch
@serwist/nexthooks into the webpack build; check Serwist's documentation for Turbopack support in your Next.js version.- Do not precache server-rendered pages that contain user data.
- Server Components and streaming work fine behind a network-first navigation strategy.