From 3afa2c79c5f0e61945a57cd625cb57d2ee70fbaf Mon Sep 17 00:00:00 2001 From: BenjaminHarding Date: Wed, 23 Oct 2019 10:58:38 +1300 Subject: [PATCH 1/6] Update to only set the original fetch callback --- src/raygun.network-tracking.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/raygun.network-tracking.js b/src/raygun.network-tracking.js index 95103114..05ad92da 100644 --- a/src/raygun.network-tracking.js +++ b/src/raygun.network-tracking.js @@ -136,7 +136,9 @@ window.raygunNetworkTrackingFactory = function(window, Raygun) { // This results in doubled up breadcrumbs // Can't reliably detect when it has been polyfilled but no IE version supports fetch // So if this is IE, don't hook into fetch - if (typeof window.fetch === 'function' && typeof window.fetch.polyfill === 'undefined' && !Raygun.Utilities.isIE()) { + var originalFetch = window.__raygunOriginalFetch || window.fetch; + + if (typeof originalFetch === 'function' && typeof originalFetch.polyfill === 'undefined' && !Raygun.Utilities.isIE()) { /** * Two window objects can be defined inside the installation code snippets that users inject into their page when using Raygun4JS. @@ -146,7 +148,6 @@ window.raygunNetworkTrackingFactory = function(window, Raygun) { * window.__raygunOriginalFetch - the window.fetch method as of when the code snippet was executed * window.__raygunFetchCallback - a callback which is executed when the code snippet fetch method is called */ - var originalFetch = window.__raygunOriginalFetch || window.fetch; var processFetch = function() { var fetchInput = arguments[0]; @@ -235,8 +236,11 @@ window.raygunNetworkTrackingFactory = function(window, Raygun) { return promise; }; - window.fetch = processFetch; - window.__raygunFetchCallback = processFetch; + if(!!window.__raygunOriginalFetch) { + window.__raygunFetchCallback = processFetch; + } else { + window.fetch = processFetch; + } disableFetchLogging = function() { window.fetch = originalFetch; From b4f7a0e562470939d66fc8daa0f20a49377376ec Mon Sep 17 00:00:00 2001 From: BenjaminHarding Date: Thu, 24 Oct 2019 12:26:58 +1300 Subject: [PATCH 2/6] Update CHANGELOG and package version --- CHANGELOG.md | 3 +++ bower.json | 2 +- package.json | 2 +- raygun4js.nuspec | 2 +- 4 files changed, 6 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a20769b9..f9483cbe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +* v2.18.1 + - Fixes an issue with how the network tracking util integrates with the fetch snippet + * v2.18.0 - Missing XHR timings are no longer tracked by default. Instead a configuration option exists to enable these via 'captureMissingRequests' - Set the maximum duration of missing XHR calls to 5 minutes diff --git a/bower.json b/bower.json index 6948dbff..e165d23d 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "raygun4js", - "version": "2.18.0", + "version": "2.18.1", "homepage": "http://raygun.io", "authors": [ "Mindscape " diff --git a/package.json b/package.json index 6e5e784a..c17da4e4 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ ], "title": "Raygun4js", "description": "Raygun.io plugin for JavaScript", - "version": "2.18.0", + "version": "2.18.1", "homepage": "https://github.com/MindscapeHQ/raygun4js", "author": { "name": "MindscapeHQ", diff --git a/raygun4js.nuspec b/raygun4js.nuspec index 4f6673f1..ab11ae06 100644 --- a/raygun4js.nuspec +++ b/raygun4js.nuspec @@ -2,7 +2,7 @@ raygun4js - 2.18.0 + 2.18.1 Raygun4js Mindscape Limited Mindscape Limited From e55d91467136e7844c3a1dd92c012d3270518c6e Mon Sep 17 00:00:00 2001 From: BenjaminHarding Date: Thu, 24 Oct 2019 16:41:00 +1300 Subject: [PATCH 3/6] Move comments --- src/raygun.network-tracking.js | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/raygun.network-tracking.js b/src/raygun.network-tracking.js index 05ad92da..d31dded2 100644 --- a/src/raygun.network-tracking.js +++ b/src/raygun.network-tracking.js @@ -132,22 +132,23 @@ window.raygunNetworkTrackingFactory = function(window, Raygun) { } var disableFetchLogging = function() {}; + + /** + * Two window objects can be defined inside the installation code snippets that users inject into their page when using Raygun4JS. + * These are used to intercept the original fetch method before a reference to it can be made. + * Because if a stored reference to the fetch method is made, we cannot get the status code from that point onwards. + * + * window.__raygunOriginalFetch - the window.fetch method as of when the code snippet was executed + * window.__raygunFetchCallback - a callback which is executed when the code snippet fetch method is called + */ + var originalFetch = window.__raygunOriginalFetch || window.fetch; + // If fetch has been polyfilled we don't want to hook into it as it then uses XMLHttpRequest // This results in doubled up breadcrumbs // Can't reliably detect when it has been polyfilled but no IE version supports fetch // So if this is IE, don't hook into fetch - var originalFetch = window.__raygunOriginalFetch || window.fetch; - if (typeof originalFetch === 'function' && typeof originalFetch.polyfill === 'undefined' && !Raygun.Utilities.isIE()) { - /** - * Two window objects can be defined inside the installation code snippets that users inject into their page when using Raygun4JS. - * These are used to intercept the original fetch method before a reference to it can be made. - * Because if a stored reference to the fetch method is made, we cannot get the status code from that point onwards. - * - * window.__raygunOriginalFetch - the window.fetch method as of when the code snippet was executed - * window.__raygunFetchCallback - a callback which is executed when the code snippet fetch method is called - */ var processFetch = function() { var fetchInput = arguments[0]; From 672499bbb0881d594d4b972ed744be8dc37ba663 Mon Sep 17 00:00:00 2001 From: BenjaminHarding Date: Thu, 24 Oct 2019 17:05:52 +1300 Subject: [PATCH 4/6] Add additional test for fetch override --- .../rumReferencedFetchWithFetchSnippet.html | 20 ++++++++++- tests/specs/v2/rumXhrStatusTracking.js | 36 +++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/tests/fixtures/v2/rumReferencedFetchWithFetchSnippet.html b/tests/fixtures/v2/rumReferencedFetchWithFetchSnippet.html index a627d05c..ee51d9a7 100644 --- a/tests/fixtures/v2/rumReferencedFetchWithFetchSnippet.html +++ b/tests/fixtures/v2/rumReferencedFetchWithFetchSnippet.html @@ -47,7 +47,25 @@ } })(window,document,"script","/dist/raygun.js","rg4js"); - var referencedFetch = window.fetch; + /** + * Overrides the fetch method to assert that once raygun4js has loaded, + * subsequent overrides to the window.fetch method still work + */ + window.__completedCalls = []; + + if (typeof window.fetch !== 'undefined') { + var originalFetch = window.fetch; + + window.fetch = function() { + var url = arguments[0]; + + return originalFetch.apply(null, arguments).then(function() { + completedCalls.push(url); + }); + }; + } + + var referencedFetch = window.fetch; diff --git a/tests/specs/v2/rumXhrStatusTracking.js b/tests/specs/v2/rumXhrStatusTracking.js index dfa65a0e..af934922 100644 --- a/tests/specs/v2/rumXhrStatusTracking.js +++ b/tests/specs/v2/rumXhrStatusTracking.js @@ -94,6 +94,42 @@ describe("RUM status code tracking", function() { checkStatusCodes(); }); + + it('overriden fetch methods are stilled called', () => { + browser.url('http://localhost:4567/fixtures/v2/rumReferencedFetchWithFetchSnippet.html'); + + browser.pause(1000); + + var supportsFetch = browser.execute(function() { + return window.supportsFetch; + }).value; + + if (!supportsFetch) { + return; + } + + browser.pause(34000); + + var completedCalls = browser.execute(function () { + return window.__completedCalls; + }).value; + + if (completedCalls.length < 4) { + fail("test did not wait long enough for ajax requests to be sent to Raygun"); + } + + var expectedCalls = [ + 'foo.html', + 'rumXhrStatusCodes.html', + 'rumXhrStatusCodes.html?foo=bar', + 'http://localhost:4567/fixtures/v2/rumXhrStatusCodes.html' + ]; + + for (var i = 0; i < expectedCalls.length; i++) { + var url = expectedCalls[i]; + expect(completedCalls.indexOf(url)).not.toBe(-1); + } + } }); }); From a2fa18091e242aa47602892cfdbabb3c5b43bcaf Mon Sep 17 00:00:00 2001 From: BenjaminHarding Date: Thu, 24 Oct 2019 17:13:53 +1300 Subject: [PATCH 5/6] Fix test --- tests/specs/v2/rumXhrStatusTracking.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/specs/v2/rumXhrStatusTracking.js b/tests/specs/v2/rumXhrStatusTracking.js index af934922..ebcf3a0b 100644 --- a/tests/specs/v2/rumXhrStatusTracking.js +++ b/tests/specs/v2/rumXhrStatusTracking.js @@ -129,7 +129,7 @@ describe("RUM status code tracking", function() { var url = expectedCalls[i]; expect(completedCalls.indexOf(url)).not.toBe(-1); } - } + }); }); }); From dd4a8aa48dfb11e914b8710be93915eaf0057f6d Mon Sep 17 00:00:00 2001 From: BenjaminHarding Date: Thu, 24 Oct 2019 17:26:05 +1300 Subject: [PATCH 6/6] Append to completed calls to correct variable --- tests/fixtures/v2/rumReferencedFetchWithFetchSnippet.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/fixtures/v2/rumReferencedFetchWithFetchSnippet.html b/tests/fixtures/v2/rumReferencedFetchWithFetchSnippet.html index ee51d9a7..d1297ab4 100644 --- a/tests/fixtures/v2/rumReferencedFetchWithFetchSnippet.html +++ b/tests/fixtures/v2/rumReferencedFetchWithFetchSnippet.html @@ -60,7 +60,7 @@ var url = arguments[0]; return originalFetch.apply(null, arguments).then(function() { - completedCalls.push(url); + window.__completedCalls.push(url); }); }; }