Fix: Runtime VAPID key fetching to avoid build-time environment dependency
This commit is contained in:
parent
ebd1f67f74
commit
79377fb210
7
src/app/api/notifications/config/route.ts
Normal file
7
src/app/api/notifications/config/route.ts
Normal file
@ -0,0 +1,7 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
|
||||
export async function GET() {
|
||||
return NextResponse.json({
|
||||
publicKey: process.env.NEXT_PUBLIC_VAPID_PUBLIC_KEY
|
||||
});
|
||||
}
|
||||
@ -52,9 +52,20 @@ export function useNotifications(reminderSettings: ReminderSettings) {
|
||||
throw new Error('Service Worker not ready');
|
||||
}
|
||||
|
||||
const publicVapidKey = process.env.NEXT_PUBLIC_VAPID_PUBLIC_KEY;
|
||||
let publicVapidKey = process.env.NEXT_PUBLIC_VAPID_PUBLIC_KEY;
|
||||
|
||||
// If missing from build-time env, fetch it from runtime API
|
||||
if (!publicVapidKey) {
|
||||
throw new Error('Missing VAPID key');
|
||||
console.log('VAPID key missing from build-time env, fetching from runtime API...');
|
||||
const configRes = await fetch('/api/notifications/config');
|
||||
if (configRes.ok) {
|
||||
const config = await configRes.json() as { publicKey?: string };
|
||||
publicVapidKey = config.publicKey || '';
|
||||
}
|
||||
}
|
||||
|
||||
if (!publicVapidKey) {
|
||||
throw new Error('Missing VAPID key (neither in build env nor runtime API). Please ensure NEXT_PUBLIC_VAPID_PUBLIC_KEY is set in Cloudflare secrets.');
|
||||
}
|
||||
|
||||
// 1. Ensure we have a subscription
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user