#push service error - webpush + Nextjs + serwist,
1 messages · Page 1 of 1 (latest)
Based on the official Next.js documentation for implementing web push notifications and PWAs, here are some key points and troubleshooting steps that may help you resolve your issue with webpush + Next.js + Serwist:
-
Service Worker Location
The service worker file should be placed in thepublic/directory aspublic/sw.js, not insrc/app/. This is important because the browser expects the service worker to be served from the root of your domain (e.g.,/sw.js). If you place it insrc/app/, it will not be accessible at the expected URL, and registration may silently fail or not behave as intended. The official example shows the service worker inpublic/sw.js:// public/sw.js self.addEventListener('push', function (event) { // ... }); // ... -
Registering the Service Worker
When registering the service worker in your client code, make sure you are referencing/sw.js(not/src/app/sw.js). Example:await navigator.serviceWorker.register('/sw.js', { scope: '/', updateViaCache: 'none', }); -
VAPID Keys and Environment Variables
Ensure your VAPID keys are correctly set in your environment variables and referenced in both your server (for web-push) and client (for pushManager.subscribe). The documentation shows:NEXT_PUBLIC_VAPID_PUBLIC_KEY=your_public_key_here VAPID_PRIVATE_KEY=your_private_key_here
-
Server Actions and Subscription Handling
The server action for sending notifications should use the correct subscription object. In production, you should store subscriptions in a database, but for testing, you can use an in-memory variable as shown in the docs. Make sure you are serializing/deserializing the subscription object correctly between client and server. -
Serwist Integration
If you are using Serwist for offline support, ensure your service worker is still accessible at/sw.jsand that your Next.js config is set up to output the service worker to thepublic/directory. The documentation notes that Serwist requires custom webpack configuration and that you should follow their integration guide closely. If the service worker is being generated elsewhere, you may need to copy or move it topublic/as part of your build process. -
Testing and Debugging
-
Test your service worker registration and push notification flow locally using HTTPS (
next dev --experimental-https). -
Check the browser console for errors related to service worker registration, push subscription, or notification display.
-
Make sure your service worker is not cached and is always up to date. You can set appropriate headers in
next.config.js:{ source: '/sw.js', headers: [ { key: 'Content-Type', value: 'application/javascript; charset=utf-8' }, { key: 'Cache-Control', value: 'no-cache, no-store, must-revalidate' }, { key: 'Content-Security-Policy', value: "default-src 'self'; script-src 'self'" }, ], }
-
Summary:
- Move your
sw.jsto thepublic/directory. - Register the service worker as
/sw.js. - Double-check your VAPID keys and environment variables.
- Ensure your subscription handling matches the official example.
- If using Serwist, ensure the generated service worker ends up in
public/as/sw.js. - Set proper headers for
/sw.jsin your Next.js config.
If you follow these steps and still encounter errors, please provide the exact error message you are seeing, as the knowledge sources do not contain information about specific error messages related to your code or the sourceb.in links you provided. If you can share the error text, I can help further based on the documentation and known issues.
How to build a Progressive Web Application (PWA) with Next.js
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@flint ermine Kapa.ai is still learning and improving, please let me know how I did by reacting below.
generated the web push keys through web-push generate-vapid-keys