"Royal-Mail-shipping-integration-and-test-improvements"

This commit is contained in:
sysadmin
2025-09-08 03:53:28 +01:00
parent be4d797c6c
commit bcca00ab39
9 changed files with 1002 additions and 106 deletions

View File

@@ -336,7 +336,11 @@ class PWAManager {
applicationServerKey: this.urlBase64ToUint8Array(this.vapidPublicKey)
});
// Send subscription to server
// Send subscription to server with timeout
console.log('PWA: Sending subscription to server...');
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 10000); // 10 second timeout
const response = await fetch('/api/push/subscribe', {
method: 'POST',
headers: {
@@ -347,8 +351,12 @@ class PWAManager {
p256dh: btoa(String.fromCharCode(...new Uint8Array(subscription.getKey('p256dh')))),
auth: btoa(String.fromCharCode(...new Uint8Array(subscription.getKey('auth'))))
}),
credentials: 'same-origin'
credentials: 'same-origin',
signal: controller.signal
});
clearTimeout(timeoutId);
console.log('PWA: Server response received:', response.status, response.statusText);
if (response.ok) {
this.pushSubscription = subscription;
@@ -436,7 +444,14 @@ class PWAManager {
subscribeBtn.innerHTML = '<i class="fas fa-spinner fa-spin me-1"></i>Subscribing...';
try {
await this.subscribeToPushNotifications();
// Add timeout to prevent infinite hanging
const subscriptionPromise = this.subscribeToPushNotifications();
const timeoutPromise = new Promise((_, reject) =>
setTimeout(() => reject(new Error('Push subscription timed out after 15 seconds. This may be due to network connectivity or browser push service issues.')), 15000)
);
await Promise.race([subscriptionPromise, timeoutPromise]);
this.showNotification('Push notifications enabled!', {
body: 'You will now receive notifications for new orders and updates.'
});
@@ -447,6 +462,8 @@ class PWAManager {
let userMessage = error.message;
if (error.message.includes('permission')) {
userMessage = 'Please allow notifications when your browser asks, then try again.';
} else if (error.message.includes('timeout')) {
userMessage = 'Push notification setup timed out. This may be due to network or browser issues. Please try again or check your internet connection.';
}
alert('Failed to enable push notifications: ' + userMessage);