try another PWA plugin,

debug .env on TEST/STAGING
improve /notify API
This commit is contained in:
Dobromir Popov
2024-05-06 15:59:42 +03:00
parent 9056ca4f4a
commit db31066724
11 changed files with 816 additions and 247 deletions

View File

@ -82,14 +82,29 @@ function PwaManager() {
try {
e.preventDefault();
if (!navigator.serviceWorker) {
console.error('Service worker is not supported by this browser.');
return;
}
const registration = await navigator.serviceWorker.ready;
if (!registration) {
console.error('Service worker registration not found.');
registration
return;
}
let vapidPublicKey = process.env.NEXT_PUBLIC_VAPID_PUBLIC_KEY;
if (!vapidPublicKey) {
// Fetch the public key from the server if not present in env variables
const response = await fetch('/api/notify', { method: 'GET' });
vapidPublicKey = await response.text();
if (!vapidPublicKey) {
throw new Error("Failed to fetch VAPID public key from server.");
}
}
const sub = await registration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: base64ToUint8Array(process.env.NEXT_PUBLIC_VAPID_PUBLIC_KEY)
applicationServerKey: base64ToUint8Array(vapidPublicKey)
});
// Call your API to save subscription data on server
setSubscription(sub);