Skip to content

Commit

Permalink
AutoXHR: Don't wait for XHRs that .open() but don't .send()
Browse files Browse the repository at this point in the history
  • Loading branch information
querymetrics authored and nicjansma committed Apr 3, 2018
1 parent 3bfe3d7 commit 583536b
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 7 deletions.
14 changes: 7 additions & 7 deletions plugins/auto-xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -1225,13 +1225,6 @@
// sure that we don't track this as a new request, or add additional
// event listeners
if (!opened) {
if (singlePageApp && handler.watch && !alwaysSendXhr) {
// If this is a SPA and we're already watching for resources due
// to a route change or other interesting event, add this to the
// current event.
handler.add_event_resource(resource);
}

if (async) {
addListener("readystatechange");
}
Expand Down Expand Up @@ -1278,6 +1271,13 @@
BOOMR.fireEvent("xhr_send", req);
resource.timing.requestStart = BOOMR.now();

if (singlePageApp && handler.watch && !alwaysSendXhr) {
// If this is a SPA and we're already watching for resources due
// to a route change or other interesting event, add this to the
// current event.
handler.add_event_resource(resource);
}

// call the original send method unless there was an error
// during .open
if (typeof resource.status === "undefined" ||
Expand Down
56 changes: 56 additions & 0 deletions tests/page-templates/05-angular/112-autoxhr-open-without-send.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<%= header %>
<%= boomerangScript %>
<base href="/pages/05-angular/" />
<script src="../../vendor/angular/angular.js"></script>
<script src="../../vendor/angular-resource/angular-resource.js"></script>
<script src="../../vendor/angular-route/angular-route.js"></script>
<script>
window.angular_imgs = [3000];

window.angular_html5_mode = false;

// view a widget then come back so debugging (F5) is easier
window.angular_nav_routes = ["/widgets/1", "/"];
</script>
<script src="support/app.js"></script>
<div ng-app="app">
<div ng-view>
</div>
</div>
<script src="112-autoxhr-open-without-send.js" type="text/javascript"></script>
<script>
var img, xhr, beaconCount = 0;
BOOMR.subscribe("onbeacon", function(vars) {
beaconCount++;
if (beaconCount === 3) {
setTimeout(function() {
xhr = new XMLHttpRequest();
xhr.open("GET", "/delay?delay=100&file=/pages/05-angular/support/widgets.json&rnd=" + Math.random());
xhr.addEventListener("load", function() {
img = document.createElement("img");
img.src = "/delay?delay=1000&id=xhr&file=/pages/05-angular/support/img.jpg";
document.body.appendChild(img);
xhr2 = new XMLHttpRequest();
xhr2.open("GET", "/delay?delay=100&file=/pages/05-angular/support/widgets.json&rnd=" + Math.random());
// Don't call send on xhr
});
xhr.send(null);
}, 100);
}
});
BOOMR_test.init({
testAfterOnBeacon: 4,
Angular: {
enabled: true
},
instrument_xhr: true,
autorun: false
});

// Timeout after 30s in case auto xhr is still waiting for a resource
window.timerid = setTimeout(function() {
BOOMR_test.runTests();
}, 30000);

</script>
<%= footer %>
40 changes: 40 additions & 0 deletions tests/page-templates/05-angular/112-autoxhr-open-without-send.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*eslint-env mocha*/
/*global BOOMR,BOOMR_test,describe,it*/

describe("e2e/05-angular/112-autoxhr-open-without-send", function() {
var tf = BOOMR.plugins.TestFramework;
var t = BOOMR_test;

it("Should pass basic beacon validation", function(done) {
t.validateBeaconWasSent(done);
clearTimeout(window.timerid);
});

it("Should have sent three beacons", function() {
assert.equal(tf.beacons.length, 4);
});

it("Should have sent the first beacon as http.initiator = spa_hard", function() {
assert.equal(tf.beacons[0]["http.initiator"], "spa_hard");
});

it("Should have beacon 1 as a spa_hard", function() {
if (t.isMutationObserverSupported() || t.isResourceTimingSupported()) {
var b = tf.beacons[0];
assert.equal(b["http.initiator"], "spa_hard");
}
});

it("Should have beacon 2 as a spa", function() {
if (t.isMutationObserverSupported() || t.isResourceTimingSupported()) {
var b = tf.beacons[1];
assert.equal(b["http.initiator"], "spa");
}
});

it("Should have beacon 3 as a spa", function() {
var b = tf.beacons[3];
assert.equal(b["http.initiator"], "xhr");
});
});

0 comments on commit 583536b

Please sign in to comment.