try another PWA plugin,
debug .env on TEST/STAGING improve /notify API
This commit is contained in:
@ -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);
|
||||
|
Reference in New Issue
Block a user