To add web push notifications to a Next.js site, put a service worker file in the public folder, generate VAPID keys, and ask for permission from a button click. Then save each push subscription in a database and send messages from a Server Action or API route with the web-push library. Or use a push platform’s script and service worker.
Next.js web push notifications follow the same W3C standards as any other site, so there’s nothing Next.js-specific about delivery. What Next.js changes is where files live and where server code runs. Below I cover both routes, the DIY build and the platform route, and the mistakes I see most often.
What do you need to add web push to Next.js?
You need five pieces, and the official Next.js PWA guide walks through each of them for the App Router.
| Piece | Where it goes in Next.js | Purpose |
|---|---|---|
| HTTPS | Your hosting (localhost is fine for testing) | Push and service workers need a secure context |
| Service worker | A file in /public, served from your domain | Receives push events and shows notifications |
| VAPID key pair | Environment variables | Identifies your server to push services |
| Subscribe UI | A client component | Requests permission on a click and creates the subscription |
| Sender | A Server Action or route handler | Stores subscriptions and sends messages |
Keep the private VAPID key server-side only. In Next.js, any variable prefixed with NEXT_PUBLIC_ is exposed to the browser, so only the public key gets that prefix. My post on VAPID keys for web push explains what each key does.
How do you build it yourself, step by step?
Build it in seven steps. This is the route the Next.js guide uses, with the open-source web-push library on the server.
- Install web-push and run its generate-vapid-keys command.
- Save the keys as NEXT_PUBLIC_VAPID_PUBLIC_KEY and VAPID_PRIVATE_KEY in your .env file.
- Create a service worker in /public that listens for push and notificationclick events.
- In a client component, register the service worker and check that PushManager is supported.
- On a button click, call pushManager.subscribe with userVisibleOnly set to true and your public key.
- Send the subscription to a Server Action that stores it in your database.
- Send messages from the server with webpush.sendNotification, then remove subscriptions that return 404 or 410.
The Next.js guide stores a single subscription in memory to keep the example short. As its own comments note, a real app needs a database, or every restart loses your subscribers. For the protocol details behind step 7, see MDN’s Push API reference.
Where should the service worker file live?
Put it in the public folder so Next.js serves it as a static file from your own domain, such as /sw.js. A service worker controls only pages within its scope, and by default the scope is the folder it’s served from, so the site root covers everything.
Don’t bundle it into your JavaScript chunks or serve it from a CDN on another domain. Browsers require the service worker to come from the same origin as your site. Also, avoid caching it aggressively, or fixes won’t reach users quickly. My guide to the service worker for web push covers scope and caching in more detail.
How do you handle permission in a React component?
Request permission only from a user action, such as a button’s onClick handler. Calling it in useEffect on page load fails on iPhone, where WebKit only shows the prompt in response to a tap, and it annoys visitors on every other browser.
Also, check for support before showing the button. Service workers and PushManager only exist in the browser, so run these checks in a client component, never during server rendering. MDN’s Notification.requestPermission() reference covers the API. For copy and timing, see my notes on push permission prompt best practices.
Does Next.js web push work on iPhone?
Yes, on iOS 16.4 and later, if the site is added to the Home Screen. For that, you need a web app manifest with display set to standalone. Next.js can generate one from an app/manifest.ts file, as its PWA guide shows.
The guide also includes an install prompt component that shows iPhone users how to add the site to the Home Screen. That banner matters, because Safari tabs can’t subscribe. For the full iPhone checklist, see how to get web push working on iPhone.
Should you build it yourself or use a push platform?
Build it yourself if you need a few transactional alerts and have developers to maintain them. Use a platform if marketing or editorial teams will send campaigns, because the DIY route gives you delivery but no dashboard.
| Need | DIY with web-push | Push platform |
|---|---|---|
| Send a simple alert from code | Good fit | Works too |
| Campaign editor, scheduling, templates | You build it | Included |
| Segments and click reports | You build it | Included |
| Handling expired subscriptions | You build it | Usually automatic |
| Ongoing cost | Developer time and hosting | Subscription or licence |
With a platform, the Next.js work is small: add the platform’s script with the next/script component, and place its service worker file in /public. Then the platform handles subscriptions and sending. For a general overview of options, see how to add push notifications to a website.
Where does LaraPush fit?
LaraPush is a self-hosted web push panel that works on any site that serves JavaScript, which includes Next.js apps. You add its script and service worker to your site, and it handles subscriptions, campaigns, scheduling, segmentation and reports. It also has a REST API with no call limits, so your Next.js server code can trigger sends. See the features overview and the setup guide.
The trade-offs: you run your own VPS, the licence covers one server, and there’s no free plan. Plans start from $499 one-time; check the pricing page for current prices. If you’re comparing tools, my roundup of push notification services covers SaaS and self-hosted options.
Frequently asked questions
Not strictly, but it’s much easier. Web push needs VAPID signing and payload encryption, which libraries like web-push handle for you. The official Next.js PWA guide uses web-push in a Server Action to send notifications.
Yes. Web push is a browser feature, so it doesn’t depend on the router. The service worker still goes in /public, the subscribe logic runs in client-side code, and an API route replaces the Server Action for saving subscriptions and sending.
Usually because permission is requested on page load instead of on a click, the site isn’t on HTTPS, or the service worker failed to register. On iPhone, the site must also be added to the Home Screen before the prompt can appear.
Yes. Browsers treat localhost as a secure context, so service workers and push work there without a certificate. iPhone testing, however, needs a real HTTPS deployment added to the Home Screen.
Yes. The public key is meant to be shared with browsers, so the NEXT_PUBLIC_ prefix is fine for it. Never prefix the private key that way, because anything with NEXT_PUBLIC_ is bundled into client-side code.




