From c986d939ec8c045645e8f174ef877a88e2809da3 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Tue, 20 May 2025 00:22:28 +0200 Subject: [PATCH] Read vapid details from runtime config Use the useRuntimeConfig interface to read vapid details on the server side. This is a more portale way to deal with loading data from the environment in Nuxt.js. --- server/api/create-event.post.ts | 2 +- server/api/modify-event.post.ts | 2 +- server/web-push.ts | 31 +++++++++++++++++++++++++------ 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/server/api/create-event.post.ts b/server/api/create-event.post.ts index db3bdbe..04b3d06 100644 --- a/server/api/create-event.post.ts +++ b/server/api/create-event.post.ts @@ -27,5 +27,5 @@ export default defineEventHandler(async (event) => { }); await broadcastUpdate(schedule); await writeSchedule(schedule); - await sendPush("New event", `${name} will start at ${start}`); + await sendPush(event, "New event", `${name} will start at ${start}`); }); diff --git a/server/api/modify-event.post.ts b/server/api/modify-event.post.ts index abceecb..a673a2b 100644 --- a/server/api/modify-event.post.ts +++ b/server/api/modify-event.post.ts @@ -33,5 +33,5 @@ export default defineEventHandler(async (event) => { await broadcastUpdate(schedule); await writeSchedule(schedule); if (timeChanged) - await sendPush(`New time for ${name}`, `${name} will now start at ${start}`); + await sendPush(event, `New time for ${name}`, `${name} will now start at ${start}`); }); diff --git a/server/web-push.ts b/server/web-push.ts index ee5f8e3..2dd9bc8 100644 --- a/server/web-push.ts +++ b/server/web-push.ts @@ -1,13 +1,31 @@ +import type { H3Event } from "h3"; import webPush from "web-push"; import { readSubscriptions, writeSubscriptions } from "~/server/database"; -webPush.setVapidDetails( - "mailto:webmaster@hornwitser.no", - process.env.NUXT_PUBLIC_VAPID_PUBLIC_KEY!, - process.env.NUXT_VAPID_PRIVATE_KEY!, -) +let cachedVapidDetails: { + subject: string, + publicKey: string, + privateKey: string, +} | undefined; -export async function sendPush(title: string, body: string) { +function useVapidDetails(event: H3Event) { + if (cachedVapidDetails) { + return cachedVapidDetails; + } + + const runtimeConfig = useRuntimeConfig(event); + if (!runtimeConfig.public.vapidPublicKey) throw new Error("NUXT_PUBLIC_VAPID_PUBLIC_KEY not set.") + if (!runtimeConfig.vapidPrivateKey) throw new Error("NUXT_VAPID_PRIVATE_KEY not set.") + + return cachedVapidDetails = { + subject: "mailto:webmaster@hornwitser.no", + publicKey: runtimeConfig.public.vapidPublicKey, + privateKey: runtimeConfig.vapidPrivateKey, + } +} + +export async function sendPush(event: H3Event, title: string, body: string) { + const vapidDetails = useVapidDetails(event); const payload = JSON.stringify({ title, body }); const subscriptions = await readSubscriptions(); console.log(`Sending "${payload}" to ${subscriptions.length} subscribers`); @@ -23,6 +41,7 @@ export async function sendPush(title: string, body: string) { { TTL: 3600, urgency: "high", + vapidDetails, } ) } catch (err: any) {