-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-link-header.ts
53 lines (53 loc) · 1.69 KB
/
build-link-header.ts
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
// This is meant to be run from phantomjs
var webPage = require('webpage');
var page = webPage.create();
var resourcesRequested = [];
page.onResourceRequested = function (requestData) {
requestData.url = requestData.url.replace(new RegExp("http://localhost:8080"), "")
requestData.url = requestData.url.replace(new RegExp("https?://trajano.net"), "")
requestData.url = requestData.url.replace(new RegExp("http://"), "https://")
if (requestData.url.match(/\.svg$/)) {
requestData.as = "image"
}
if (requestData.url.match(/\.css$/)) {
requestData.as = "style"
}
if (requestData.url == "https://fonts.googleapis.com/icon?family=Material+Icons") {
requestData.as = "style"
}
if (requestData.url.match(/\.png$/)) {
requestData.as = "image"
}
if (requestData.url.match(/\.jpg$/)) {
requestData.as = "image"
}
if (requestData.url.match(/\.js$/)) {
requestData.as = "script"
}
if (requestData.url.match(/\.ttf(\?.*)?$/)) {
requestData.as = "font"
}
if (requestData.url == '/') {
return
}
if (requestData.url.match(/&/)) {
return
}
// do not preload stuff from wp-content, they could be responsive so let the browser decide when to get them.
if (requestData.url.match(/wp-content/)) {
return
}
resourcesRequested.push(requestData);
};
page.open('https://trajano.net', function (status) {
var s = ""
resourcesRequested.forEach(function (resource, i) {
if (resource.url.match(/^\//)) {
s += ("Header merge Link \"<" + resource.url + ">;rel=preload;as=" + resource.as + "\"\n")
} else {
console.log("<link rel=\"preload\" href=\"" + resource.url + "\" as=\"" + resource.as + "\">")
}
})
console.log(s)
phantom.exit();
});