Nuxt is based on Vue.js and I find their building blocks to be much neater compared to the React based Next.js.
56 lines
1.6 KiB
TypeScript
56 lines
1.6 KiB
TypeScript
import { readFile, writeFile } from "node:fs/promises";
|
|
import webPush from "web-push";
|
|
|
|
webPush.setVapidDetails(
|
|
"mailto:webmaster@hornwitser.no",
|
|
process.env.NUXT_PUBLIC_VAPID_PUBLIC_KEY!,
|
|
process.env.NUXT_VAPID_PRIVATE_KEY!,
|
|
)
|
|
|
|
export async function sendPush(title: string, body: string) {
|
|
const payload = JSON.stringify({ title, body });
|
|
let subscriptions: PushSubscriptionJSON[];
|
|
try {
|
|
subscriptions = JSON.parse(await readFile("push-subscriptions.json", "utf-8"));
|
|
} catch (err: any) {
|
|
if (err.code !== "ENOENT") {
|
|
console.log(`Dropping "${payload}", no push subscribers`);
|
|
return;
|
|
}
|
|
subscriptions = [];
|
|
}
|
|
console.log(`Sending "${payload}" to ${subscriptions.length} subscribers`);
|
|
const removeIndexes = [];
|
|
for (let index = 0; index < subscriptions.length; index += 1) {
|
|
const subscription = subscriptions[index];
|
|
try {
|
|
await webPush.sendNotification(
|
|
subscription as webPush.PushSubscription,
|
|
payload,
|
|
{
|
|
TTL: 3600,
|
|
urgency: "high",
|
|
}
|
|
)
|
|
} catch (err: any) {
|
|
console.error("Received error sending push notice:", err.message, err?.statusCode)
|
|
console.error(err);
|
|
if (err?.statusCode >= 400 && err?.statusCode < 500) {
|
|
removeIndexes.push(index)
|
|
}
|
|
}
|
|
}
|
|
if (removeIndexes.length) {
|
|
console.log(`Removing indexes ${removeIndexes} from subscriptions`)
|
|
removeIndexes.reverse();
|
|
for (const index of removeIndexes) {
|
|
subscriptions.splice(index, 1);
|
|
}
|
|
await writeFile(
|
|
"push-subscriptions.json",
|
|
JSON.stringify(subscriptions, undefined, "\t"),
|
|
"utf-8"
|
|
);
|
|
}
|
|
console.log("Push notices sent");
|
|
}
|