fix: lazy-initialize VAPID to avoid build-time crash when env vars absent
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
+10
-5
@@ -1,16 +1,21 @@
|
||||
import webpush from "web-push";
|
||||
import { db, pushSubscriptions, eq } from "@epicure/db";
|
||||
|
||||
webpush.setVapidDetails(
|
||||
"mailto:contact@epicure.app",
|
||||
process.env.NEXT_PUBLIC_VAPID_PUBLIC_KEY!,
|
||||
process.env.VAPID_PRIVATE_KEY!
|
||||
);
|
||||
let vapidInitialized = false;
|
||||
function ensureVapid() {
|
||||
if (vapidInitialized) return;
|
||||
const pub = process.env.NEXT_PUBLIC_VAPID_PUBLIC_KEY;
|
||||
const priv = process.env.VAPID_PRIVATE_KEY;
|
||||
if (!pub || !priv) throw new Error("VAPID keys not configured");
|
||||
webpush.setVapidDetails("mailto:contact@epicure.app", pub, priv);
|
||||
vapidInitialized = true;
|
||||
}
|
||||
|
||||
export async function sendPushNotification(
|
||||
userId: string,
|
||||
notification: { title: string; body: string; url?: string }
|
||||
): Promise<void> {
|
||||
ensureVapid();
|
||||
const subs = await db
|
||||
.select()
|
||||
.from(pushSubscriptions)
|
||||
|
||||
Reference in New Issue
Block a user