Skip to content

Commit

Permalink
Merge pull request #622 from donejs/preload-as
Browse files Browse the repository at this point in the history
Sets the preload link "as"
  • Loading branch information
matthewp authored Dec 13, 2018
2 parents 7683f74 + 6bf97da commit fde8a85
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 6 deletions.
3 changes: 2 additions & 1 deletion lib/util/set_http_version.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
var isHTTP1Request = require("./is_http1_request");

module.exports = function(data, request) {
module.exports = function(data, request, stream) {
var isH1 = isHTTP1Request(request);
data.httpVersion = isH1 ? "h1" : "h2";
data.isHTTP1 = isH1;
data.isHTTP2 = !isH1;
data.pushAllowed = !!(data.isHTTP2 && stream.pushAllowed);
};
7 changes: 6 additions & 1 deletion test/helpers/incremental.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ exports.findMutationDoc = function(node) {
var srcdoc = node.getAttribute("srcdoc");
var str = he.decode(he.decode(srcdoc));
return domHelpers.dom(str);
}
};

exports.extractIframeDoc = function(html) {
var node = domHelpers.dom(html);
return exports.findMutationDoc(node);
};

exports.extractInstructionsURL = function(html) {
var node = domHelpers.dom(html);
Expand Down
1 change: 1 addition & 0 deletions test/inc_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ exports.mock = function(url, expectedPushes){
var H2Stream = class extends Duplex {
constructor(options) {
super(options);
this.pushAllowed = true;
}
// We only need this if we have a POST body
_read() {}
Expand Down
11 changes: 11 additions & 0 deletions test/incremental_h1_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,16 @@ describe("Incremental rendering with HTTP/1", function(){
assert.equal(oh.type, "insert");
assert.equal(oh.node.nodeName, "ORDER-HISTORY");
});

it("Includes the preload link in the iframe document", function(){
var doc = helpers.extractIframeDoc(this.htmlResponse.body);
var link = helpers.find(doc, function(node) {
return node.getAttribute && node.getAttribute("rel") === "preload";
});

assert.ok(link, "Preload link exists");
assert.equal(link.getAttribute("as"), "fetch", "has the correct 'as'");
assert.equal(link.getAttribute("crossorigin"), "anonymous", "set as anonymous");
});
});
});
4 changes: 2 additions & 2 deletions zones/push-mutations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = function(requestOrHeaders, stream,
}

return function(data){
setHTTPVersion(data, requestOrHeaders);
setHTTPVersion(data, requestOrHeaders, stream);
var instrStream;

return {
Expand All @@ -22,7 +22,7 @@ module.exports = function(requestOrHeaders, stream,

created: function(){
// If this is HTTP/1 a preload link is added.
if(data.isHTTP1) {
if(!data.pushAllowed) {
pushStreams.set(url, data.mutations);
return;
}
Expand Down
6 changes: 4 additions & 2 deletions zones/push-mutations/reattach.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ module.exports = function(url){
appendToHead(fakeDoc, document.createComment("iframe placeholder"));

// Preload
if(data.isHTTP1) {
if(!data.pushAllowed) {
var link = data.document.createElement("link");
link.setAttribute("rel", "preload");
link.setAttribute("as", "fetch");
link.setAttribute("crossorigin", "anonymous");
link.setAttribute("href", url);
appendToHead(fakeDoc, link);
}
Expand Down Expand Up @@ -89,7 +91,7 @@ module.exports = function(url){
function injectStuff() {
let doc = data.document;
injectIntoHead(doc, makeIframe(doc, data));
if(data.isHTTP1) {
if(!data.pushAllowed) {
// Preload link placeholder
injectIntoHead(doc, doc.createComment("autorender-keep preload placeholder"));
}
Expand Down

0 comments on commit fde8a85

Please sign in to comment.