Push notifications for embeds

Allow readers to subscribe to liveblog alerts and get notified the moment new updates go out.

1. Prerequisites

  • Serve your site over HTTPS.
  • Target browsers must support the Push API and Service Workers.

2. Configure VAPID keys

Generate a VAPID keypair (using web-push or similar) and add the following environment variables:

  • NEXT_PUBLIC_VAPID_PUBLIC_KEY
  • VAPID_PRIVATE_KEY
  • VAPID_SUBJECT (usually a mailto: contact)

3. Service worker

The worker lives at /push-sw.js (served from public/). When the embed mounts, it registers the worker and shows a bell UI so readers can opt in or out.

4. Subscription storage

Subscriptions are stored in the push_subscriptions table. The embed handles create/update/delete via the public API.

5. Sending notifications

  • Publishing a liveblog update can automatically fan out pushes via src/lib/notifications/push.ts.
  • Manual broadcasts are available through POST /api/liveblogs/<id>/broadcast/notify.

Troubleshooting

  • Double-check that NEXT_PUBLIC_SITE_URL points to the HTTPS origin your embeds run on.
  • Browsers require a user gesture before showing notification prompts.
  • Expired/404 subscriptions are removed automatically during send—no manual cleanup needed.