Skip to content

Commit

Permalink
fetch assets that are missing from phantomjs
Browse files Browse the repository at this point in the history
  • Loading branch information
soulgalore committed May 24, 2013
1 parent 95071a1 commit 60785b1
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/phantomjs/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,57 @@ urls.forEach(function (url) {

comps.forEach(function (comp) {
var res = resources[comp.href] || {};
// if the component hasn't been fetched by phantomjs but discovered by yslow
if (res.response === undefined) {
try {
var headerName, h, i, len, m, startTime, endTime,
reHeader = /^([^:]+):\s*([\s\S]+)$/,
headers, response = new Object(), request = new Object();

// fetch the asset
xhr = new XMLHttpRequest();
startTime = new Date().getTime();
xhr.open('GET', ys.util.makeAbsoluteUrl(comp.href, comp.base), false);
xhr.send();
endTime = new Date().getTime();
headers = xhr.getAllResponseHeaders();
h = headers.split('\n');

// fake the request
request.headers = [];
request.url = ys.util.makeAbsoluteUrl(comp.href, comp.base);
request.method = "GET";
request.time="2013-05-22T20:40:33.381Z";

// setup the response
// real values will be added to the component
// from the header
response.bodySize = "-1";
response.contentType = "";
response.headers = [];
response.id = "-1";
response.redirectURL = null;
response.stage = "end";
response.status = xhr.status;
response.time = endTime - startTime;
response.url = ys.util.makeAbsoluteUrl(comp.href, comp.base);

// get the headers
h = headers.split('\n');
for (i = 0, len = h.length; i < len; i += 1) {
m = reHeader.exec(h[i]);
if (m) {
response.headers.push({"name":m[1], "value":m[2]});
}
}

res.response = response;
res.request = request;

} catch (err) {
console.log(err);
}
}

cset.addComponent(
comp.href,
Expand Down

0 comments on commit 60785b1

Please sign in to comment.