Port application from Next.js to Nuxt
Nuxt is based on Vue.js and I find their building blocks to be much neater compared to the React based Next.js.
This commit is contained in:
parent
8c8b561f1a
commit
250ca9a1ac
45 changed files with 662 additions and 1358 deletions
56
server/web-push.ts
Normal file
56
server/web-push.ts
Normal file
|
@ -0,0 +1,56 @@
|
|||
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");
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue