forked from cloudflare/netjet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
posthtml-preload.js
67 lines (60 loc) · 1.33 KB
/
posthtml-preload.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
'use strict';
module.exports = function(options, foundEntries) {
return function(tree) {
var matchers = [];
matchers.push({
tag: 'base',
attrs: {
href: true,
},
});
if (options.images) {
matchers.push({
tag: 'img',
attrs: {
src: true,
},
});
}
if (options.scripts) {
matchers.push({
tag: 'script',
attrs: {
src: true,
},
});
}
if (options.styles) {
matchers.push({
tag: 'link',
attrs: {
rel: 'stylesheet',
},
});
}
if (matchers.length) {
tree.match(matchers, function(node) {
switch (node.tag) {
case 'base':
foundEntries.push([node.attrs.href, 'base']);
break;
case 'img':
// Ensure we're not preloading an inline image
if (node.attrs.src.indexOf('data:') !== 0) {
foundEntries.push([node.attrs.src, 'image']);
}
break;
case 'script':
foundEntries.push([node.attrs.src, 'script']);
break;
case 'link':
foundEntries.push([node.attrs.href, 'style']);
break;
// no default
}
return node;
});
}
return foundEntries;
};
};