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 webpush from "web-push";
|
||||||
import { db, pushSubscriptions, eq } from "@epicure/db";
|
import { db, pushSubscriptions, eq } from "@epicure/db";
|
||||||
|
|
||||||
webpush.setVapidDetails(
|
let vapidInitialized = false;
|
||||||
"mailto:contact@epicure.app",
|
function ensureVapid() {
|
||||||
process.env.NEXT_PUBLIC_VAPID_PUBLIC_KEY!,
|
if (vapidInitialized) return;
|
||||||
process.env.VAPID_PRIVATE_KEY!
|
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(
|
export async function sendPushNotification(
|
||||||
userId: string,
|
userId: string,
|
||||||
notification: { title: string; body: string; url?: string }
|
notification: { title: string; body: string; url?: string }
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
|
ensureVapid();
|
||||||
const subs = await db
|
const subs = await db
|
||||||
.select()
|
.select()
|
||||||
.from(pushSubscriptions)
|
.from(pushSubscriptions)
|
||||||
|
|||||||
Reference in New Issue
Block a user