Skip to content

Commit 3417da5

Browse files
committed
preload in parallel to script download
1 parent 17eb5b4 commit 3417da5

File tree

3 files changed

+22
-26
lines changed

3 files changed

+22
-26
lines changed

lib/web/JsonpMainTemplatePlugin.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -283,25 +283,21 @@ class JsonpMainTemplatePlugin {
283283
"",
284284
`var chunkPreloadMap = ${JSON.stringify(chunkMap, null, "\t")}`,
285285
"",
286-
"var chunkPreloadata = chunkPreloadMap[chunkId];",
287-
"if(chunkPreloadata) {",
286+
"var chunkPreloadData = chunkPreloadMap[chunkId];",
287+
"if(chunkPreloadData) {",
288288
Template.indent([
289-
"Promise.all(promises).then(function() {",
289+
"var head = document.getElementsByTagName('head')[0];",
290+
"chunkPreloadData.forEach(function(chunkId) {",
290291
Template.indent([
291-
"var head = document.getElementsByTagName('head')[0];",
292-
"chunkPreloadata.forEach(function(chunkId) {",
292+
"if(installedChunks[chunkId] === undefined) {",
293293
Template.indent([
294-
"if(installedChunks[chunkId] === undefined) {",
295-
Template.indent([
296-
"installedChunks[chunkId] = null;",
297-
mainTemplate.hooks.linkPreload.call("", chunk, hash),
298-
"head.appendChild(link);"
299-
]),
300-
"}"
294+
"installedChunks[chunkId] = null;",
295+
mainTemplate.hooks.linkPreload.call("", chunk, hash),
296+
"head.appendChild(link);"
301297
]),
302-
"});"
298+
"}"
303299
]),
304-
"})"
300+
"});"
305301
]),
306302
"}"
307303
]);

test/configCases/web/prefetch-preload/index.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,30 @@ it("should prefetch and preload child chunks on chunk load", () => {
2121
__webpack_public_path__ = "/public/path/";
2222

2323
const promise = import(/* webpackChunkName: "chunk1" */ "./chunk1");
24-
document.head._children.length.should.be.eql(1);
24+
document.head._children.length.should.be.eql(2);
2525
const script = document.head._children[0];
2626
script._type.should.be.eql("script");
2727
should(script.src).be.eql("/public/path/chunk1.js")
2828
should(script.getAttribute("nonce")).be.eql("nonce")
2929
should(script.crossOrigin).be.eql("anonymous");
3030
should(script.onload).be.type("function");
3131

32+
let link = document.head._children[1];
33+
link._type.should.be.eql("link");
34+
should(link.rel).be.eql("preload");
35+
should(link.as).be.eql("script");
36+
should(link.href).be.eql("/public/path/chunk1-b.js");
37+
should(link.charset).be.eql("utf-8");
38+
should(link.getAttribute("nonce")).be.eql("nonce");
39+
should(link.crossOrigin).be.eql("anonymous");
40+
3241
__non_webpack_require__("./chunk1.js");
3342
script.onload();
3443

3544
return promise.then((ex) => {
3645
document.head._children.length.should.be.eql(4);
3746

38-
let link = document.head._children[1];
39-
link._type.should.be.eql("link");
40-
should(link.rel).be.eql("preload");
41-
should(link.as).be.eql("script");
42-
should(link.href).be.eql("/public/path/chunk1-b.js");
43-
should(link.charset).be.eql("utf-8");
44-
should(link.getAttribute("nonce")).be.eql("nonce");
45-
should(link.crossOrigin).be.eql("anonymous");
46-
47-
link = document.head._children[2];
47+
let link = document.head._children[2];
4848
link._type.should.be.eql("link");
4949
should(link.rel).be.eql("prefetch");
5050
should(link.href).be.eql("/public/path/chunk1-c.js");

test/statsCases/preload/expected.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
normal.js 130 bytes 1 [emitted] normal
44
preloaded2.js 127 bytes 2 [emitted] preloaded2
55
preloaded3.js 130 bytes 3 [emitted] preloaded3
6-
main.js 9.89 KiB 4 [emitted] main
6+
main.js 9.81 KiB 4 [emitted] main
77
inner.js 136 bytes 5 [emitted] inner
88
inner2.js 201 bytes 6 [emitted] inner2
99
Entrypoint main = main.js (preload: preloaded2.js preloaded.js preloaded3.js)

0 commit comments

Comments
 (0)