From ebf5ad282a35dabc2382086dfd459b9b1e7ef09f Mon Sep 17 00:00:00 2001 From: Ricardo Seromenho Date: Mon, 4 Dec 2017 15:58:21 +0000 Subject: [PATCH 1/2] Offline storage plugin Stores errors failed to get send and try to send them when Network comes back or on init --- plugins/offline-storage.js | 61 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 plugins/offline-storage.js diff --git a/plugins/offline-storage.js b/plugins/offline-storage.js new file mode 100644 index 000000000000..0121117b5b4e --- /dev/null +++ b/plugins/offline-storage.js @@ -0,0 +1,61 @@ +/** + * Offline Storage plugin + * + * Stores errors failed to get send and try to send them when + * Networkf comes back or on init + */ + +var offlineStorageKey = 'raven-js-offline-queue'; + +function offlineStoragelugin(Raven, options) { + options = options || {}; + + function processOfflineQueue() { + // Let's stop here if there's no connection + if (!navigator.onLine) { + return; + } + + try { + // Get the queue + var queue = JSON.parse(localStorage.getItem(offlineStorageKey)) || []; + + // Store an empty queue. If processing these one fails they get back to the queue + localStorage.setItem(offlineStorageKey, JSON.stringify([])); + + queue.forEach(function processOfflinePayload(data) { + // Avoid duplication verification for offline stored + // as they may try multiple times to be processed + Raven._lastData = null; + + // Try to process it again + Raven._sendProcessedPayload(data); + }); + } catch (error) { + Raven._logDebug('error', 'Raven transport failed to store offline: ', error); + } + } + + // Process queue on start + processOfflineQueue(); + + // Add event listener on onravenFailure and store error on localstorage + document.addEventListener('ravenFailure', function(event) { + if (!event.data) { + return; + } + + try { + var queue = JSON.parse(localStorage.getItem(offlineStorageKey)) || []; + queue.push(event.data || Raven._lastData); + localStorage.setItem(offlineStorageKey, JSON.stringify(queue)); + } catch (error) { + Raven._logDebug('error', 'Raven failed to store payload offline: ', error); + } + }); + + // Add event listener on online or custom event to trigger offline queue sending + window.addEventListener(options.onlineEventName || 'online', processOfflineQueue); +} + +module.exports = offlineStoragelugin; From fcb8db298de6cbc6699e424b6c9f411fbe2d511f Mon Sep 17 00:00:00 2001 From: Ricardo Seromenho Date: Tue, 5 Dec 2017 12:51:59 +0000 Subject: [PATCH 2/2] Typo on offline storage plugin and improvements Based on PR review --- plugins/offline-storage.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/plugins/offline-storage.js b/plugins/offline-storage.js index 0121117b5b4e..8e57a336fd69 100644 --- a/plugins/offline-storage.js +++ b/plugins/offline-storage.js @@ -7,7 +7,7 @@ var offlineStorageKey = 'raven-js-offline-queue'; -function offlineStoragelugin(Raven, options) { +function offlineStoragePlugin(Raven, options) { options = options || {}; function processOfflineQueue() { @@ -21,7 +21,7 @@ function offlineStoragelugin(Raven, options) { var queue = JSON.parse(localStorage.getItem(offlineStorageKey)) || []; // Store an empty queue. If processing these one fails they get back to the queue - localStorage.setItem(offlineStorageKey, JSON.stringify([])); + localStorage.removeItem(offlineStorageKey); queue.forEach(function processOfflinePayload(data) { // Avoid duplication verification for offline stored @@ -47,7 +47,7 @@ function offlineStoragelugin(Raven, options) { try { var queue = JSON.parse(localStorage.getItem(offlineStorageKey)) || []; - queue.push(event.data || Raven._lastData); + queue.push(event.data); localStorage.setItem(offlineStorageKey, JSON.stringify(queue)); } catch (error) { Raven._logDebug('error', 'Raven failed to store payload offline: ', error); @@ -58,4 +58,4 @@ function offlineStoragelugin(Raven, options) { window.addEventListener(options.onlineEventName || 'online', processOfflineQueue); } -module.exports = offlineStoragelugin; +module.exports = offlineStoragePlugin;