Skip to content

Commit

Permalink
ResourceTiming: Additional checks and fixes for non-supported browsers
Browse files Browse the repository at this point in the history
  • Loading branch information
nicjansma committed Apr 4, 2018
1 parent 0736a0d commit 4fe7ec0
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 13 deletions.
19 changes: 9 additions & 10 deletions plugins/auto-xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,8 @@
// Back-End: Any timeslice where a XHR or JavaScript was outstanding
// Front-End: Total Time - Back-End
//
if (!BOOMR.plugins.ResourceTiming) {
if (!BOOMR.plugins.ResourceTiming ||
!BOOMR.plugins.ResourceTiming.is_supported()) {
return;
}

Expand All @@ -541,15 +542,13 @@
var totalTime = Math.round(resource.timing.loadEventEnd - resource.timing.requestStart);

if (!resources || !resources.length) {
if (BOOMR.plugins.ResourceTiming.is_supported()) {
// If ResourceTiming is supported, but there were no entries,
// this was all Front-End time
resource.timers = {
t_resp: 0,
t_page: totalTime,
t_done: totalTime
};
}
// If ResourceTiming is supported, but there were no entries,
// this was all Front-End time
resource.timers = {
t_resp: 0,
t_page: totalTime,
t_done: totalTime
};

return;
}
Expand Down
12 changes: 9 additions & 3 deletions plugins/restiming.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,11 @@ see: http://www.w3.org/TR/resource-timing/
}

/**
* Checks if the current execution context can haz cheezburger from the specified frame
* Checks if the current execution context can access the specified frame.
*
* Note: In Safari, this will still produce a console error message, even
* though the exception is caught.
* @param {Window} frame The frame to check if access can haz
* @return {boolean} true if true, false otherwise
*/
Expand Down Expand Up @@ -335,7 +339,7 @@ see: http://www.w3.org/TR/resource-timing/

a.href = subFrames[i].src; // Get canonical URL

entries = entries.concat(findPerformanceEntriesForFrame(frame.frames[i], false, frameOffset, depth + 1, visibleEntries[a.href]));
entries = entries.concat(findPerformanceEntriesForFrame(subFrames[i].contentWindow, false, frameOffset, depth + 1, visibleEntries[a.href]));
}
}

Expand Down Expand Up @@ -1495,7 +1499,9 @@ see: http://www.w3.org/TR/resource-timing/
return this;
}

if (p && typeof p.getEntriesByType === "function") {
if (p &&
typeof p.getEntriesByType === "function" &&
typeof window.PerformanceResourceTiming !== "undefined") {
BOOMR.subscribe("page_ready", impl.done, null, impl);
BOOMR.subscribe("prerender_to_visible", impl.prerenderToVisible, null, impl);
BOOMR.subscribe("xhr_load", impl.xhr_load, null, impl);
Expand Down
32 changes: 32 additions & 0 deletions tests/page-templates/11-restiming/08-cross-origin.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<%= header %>
<%= boomerangScript %>
<script src="08-cross-origin.js" type="text/javascript"></script>
<script src="../../vendor/resourcetiming-compression/src/resourcetiming-decompression.js" type="text/javascript"></script>
<img src="/assets/img.jpg?beforeiframe" />
<script>
BOOMR_test.init({
testAfterOnBeacon: true,
ResourceTiming: {
enabled: true
},
autorun: false,
History: {
enabled: true,
auto: true
}
});
</script>

<!-- won't be able to get any resources from this frame -->
<iframe id="cross-origin"></iframe>
<script>
var host = window.location.hostname;
var port = (parseInt(window.location.port) || 4002) + 1;
window.crossOriginPort = port;
document.getElementById("cross-origin").src = "//" + host + ":" + port + "/pages/11-restiming/support/iframe.html?cross-origin";
</script>

<!-- should be able to get resources from this one -->
<iframe id="same-origin" src="support/iframe.html?same-origin"></iframe>
<img src="/assets/img.jpg?afteriframe" />
<%= footer %>
90 changes: 90 additions & 0 deletions tests/page-templates/11-restiming/08-cross-origin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*eslint-env mocha*/
/*eslint-disable no-loop-func*/
/*global BOOMR_test,assert*/

describe("e2e/11-restiming/06-iframes", function() {
var t = BOOMR_test;
var tf = BOOMR.plugins.TestFramework;

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

it("Should have a restiming parameter on the beacon (if ResourceTiming is supported)", function() {
if (t.isResourceTimingSupported()) {
var b = tf.lastBeacon();
assert.isDefined(b.restiming);
}
});

it("Should have all of the resouces on the page", function() {
if (t.isResourceTimingSupported()) {
var b = tf.lastBeacon();

var resources = ResourceTimingDecompression.decompressResources(JSON.parse(b.restiming));
var pageResources = window.performance.getEntriesByType("resource");

for (var i = 0; i < pageResources.length; i++) {
var url = pageResources[i].name;

// skip beacon URL
if (url.indexOf("blackhole") !== -1) {
continue;
}

assert.isDefined(resources.find(function(r) {
return r.name === url;
}), "Finding " + url);
}
}
});

it("Should have the same-origin IFRAME", function() {
if (t.isResourceTimingSupported()) {
var b = tf.lastBeacon();

var resources = ResourceTimingDecompression.decompressResources(JSON.parse(b.restiming));

assert.isDefined(resources.find(function(r) {
return r.name.indexOf("support/iframe.html?same-origin") !== -1;
}), "Finding support/iframe.html?same-origin");
}
});

it("Should have the IMG from the same-origin IFRAME", function() {
if (t.isResourceTimingSupported()) {
var b = tf.lastBeacon();

var resources = ResourceTimingDecompression.decompressResources(JSON.parse(b.restiming));

assert.isDefined(resources.find(function(r) {
return r.name.indexOf("/assets/img.jpg?iframe") !== -1;
}), "Finding /assets/img.jpg?iframe in the IFRAME");
}
});

it("Should have the cross-origin IFRAME", function() {
if (t.isResourceTimingSupported()) {
var b = tf.lastBeacon();

var resources = ResourceTimingDecompression.decompressResources(JSON.parse(b.restiming));

assert.isDefined(resources.find(function(r) {
return r.name.indexOf("support/iframe.html?cross-origin") !== -1;
}), "Finding support/iframe.html?cross-origin");
}
});

it("Should not have the IMG from the cross-origin IFRAME", function() {
if (t.isResourceTimingSupported()) {
var b = tf.lastBeacon();

var resources = ResourceTimingDecompression.decompressResources(JSON.parse(b.restiming));

assert.isUndefined(resources.find(function(r) {
return r.name.indexOf("/assets/img.jpg?afteriframe") !== -1 &&
r.name.indexOf(":" + window.crossOriginPort) !== -1;
}), "Not finding /assets/img.jpg?afteriframe from cross-origin iframe");
}
});
});

0 comments on commit 4fe7ec0

Please sign in to comment.