diff --git a/CHANGELOG.md b/CHANGELOG.md index e6c97715..db1bd9d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 --> +## [3.1.2] + +### Changed +- Updated when ping happens so it only happens once, and when successful, it will store in sessionStorage and not ping again. This is to prevent multiple pings from happening on every page load. + ## [3.1.1] ### Fixed diff --git a/bower.json b/bower.json index 84384aa0..d9cc173b 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "raygun4js", - "version": "3.1.0", + "version": "3.1.2", "homepage": "http://raygun.com", "authors": [ "Mindscape " diff --git a/package.json b/package.json index c7eb99d3..3b78eec4 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ ], "title": "Raygun4js", "description": "Raygun.com plugin for JavaScript", - "version": "3.1.1", + "version": "3.1.2", "homepage": "https://github.com/MindscapeHQ/raygun4js", "author": { "name": "MindscapeHQ", diff --git a/raygun4js.nuspec b/raygun4js.nuspec index 5ede8313..c49945ac 100644 --- a/raygun4js.nuspec +++ b/raygun4js.nuspec @@ -2,7 +2,7 @@ raygun4js - 3.1.0 + 3.1.2 Raygun4js Raygun Limited Raygun Limited diff --git a/src/raygun.loader.js b/src/raygun.loader.js index ee16319a..3d8b57ec 100644 --- a/src/raygun.loader.js +++ b/src/raygun.loader.js @@ -19,10 +19,17 @@ crashReportingEnabled = false, captureUnhandledRejections; + var hasSessionStorage = false; + try { + hasSessionStorage = !!window.sessionStorage; + } catch (e) { + // sessionStorage not available + } + var metadata = { ping : { + sessionStorageItem : 'raygun4js-successful-ping', sendPing : true, - pingIntervalId : -1, failedPings : 0 }, }; @@ -230,41 +237,64 @@ }; function ping() { - if(metadata.ping.failedPings > 2) { - clearInterval(metadata.ping.pingIntervalId); - } - if(!Raygun.Options || !Raygun.Options._raygunApiKey || !Raygun.Options._raygunApiUrl){ - metadata.ping.failedPings++; - return; + return; } var url = Raygun.Options._raygunApiUrl + "/ping?apiKey=" + encodeURIComponent(Raygun.Options._raygunApiKey); var data = { - crashReportingEnabled: crashReportingEnabled ? true : false, - realUserMonitoringEnabled: realUserMonitoringEnabled ? true : false, - providerName: "raygun4js", - providerVersion: '{{VERSION}}' + crashReportingEnabled: crashReportingEnabled ? true : false, + realUserMonitoringEnabled: realUserMonitoringEnabled ? true : false, + providerName: "raygun4js", + providerVersion: '{{VERSION}}' }; + // Check if we've already pinged with the same data + if (hasSessionStorage) { + var storedData = sessionStorage.getItem(metadata.ping.sessionStorageItem); + if (storedData && storedData === JSON.stringify(data)) { + return; + } + } + fetch(url, { - method: 'POST', - headers: { - 'Content-Type': 'application/json' - }, - body: JSON.stringify(data) + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify(data) }).then(function(response) { - if (response.ok) { - metadata.ping.failedPings = 0; - } else { - // Request failed - metadata.ping.failedPings++; + if (response.ok) { + if (hasSessionStorage) { + // Record successful ping in local storage + sessionStorage.setItem(metadata.ping.sessionStorageItem, JSON.stringify(data)); } - }).catch(function() { + metadata.ping.failedPings = 0; + } else { + retryPing(metadata.ping.failedPings); metadata.ping.failedPings++; + } + }).catch(function() { + retryPing(metadata.ping.failedPings); + metadata.ping.failedPings++; }); } + var retryPing = function(failedPings) { + if (failedPings > 5) { + // Stop retrying after 5 failed attempts + return; + } + + // Generates a delay of 10/20/40/80/120 seconds + var backoffDelay = Math.min( + 10 * Math.pow(2, metadata.ping.failedPings), + 120 // 2 minutes + ) * 1000; + + // Retry after backoff delay + setTimeout(ping, backoffDelay); + }; var installGlobalExecutor = function() { window[window['RaygunObject']] = function() { @@ -325,7 +355,6 @@ if(metadata.ping.sendPing) { ping(); //call immediately - metadata.ping.pingIntervalId = setInterval(ping, 1000 * 60 * 5); //5 minutes } window[window['RaygunObject']].q = errorQueue; };