-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.js
252 lines (212 loc) · 5.92 KB
/
index.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
//
// hapi plugin for [prerender](https://github.com/prerender/prerender).
// Loosely based on [prerender-node](https://github.com/prerender/prerender-node)
//
var Url = require('url');
var Zlib = require('zlib');
var Request = require('request');
var Hoek = require('hoek');
// Declare internals
var internals = {};
// googlebot, yahoo, and bingbot are not in this list because
// we support _escaped_fragment_ and want to ensure people aren't
// penalized for cloaking.
internals.crawlerUserAgents = [
// 'googlebot',
// 'yahoo',
// 'bingbot',
'facebot',
'slackbot',
'baiduspider',
'facebookexternalhit',
'twitterbot',
'rogerbot',
'linkedinbot',
'embedly',
'quora link preview',
'showyoubot',
'outbrain',
'pinterest',
'developers.google.com/+/web/snippet'
];
internals.extensionsToIgnore = [
'.js',
'.css',
'.xml',
'.less',
'.png',
'.jpg',
'.jpeg',
'.gif',
'.pdf',
'.doc',
'.txt',
'.ico',
'.rss',
'.zip',
'.mp3',
'.rar',
'.exe',
'.wmv',
'.doc',
'.avi',
'.ppt',
'.mpg',
'.mpeg',
'.tif',
'.wav',
'.mov',
'.psd',
'.ai',
'.xls',
'.mp4',
'.m4a',
'.swf',
'.dat',
'.dmg',
'.iso',
'.flv',
'.m4v',
'.torrent',
'.svg'
];
internals.shouldShowPrerenderedPage = function (req) {
var userAgent = req.headers['user-agent'];
var bufferAgent = req.headers['x-bufferbot'];
var isRequestingPrerenderedPage = false;
if (!userAgent) { return false; }
if (req.method.toLowerCase() !== 'get') { return false; }
//if it contains _escaped_fragment_, show prerendered page
if (typeof req.url.query._escaped_fragment_ !== 'undefined') {
isRequestingPrerenderedPage = true;
}
//if it is a bot...show prerendered page
var knownBot = internals.crawlerUserAgents.some(function (crawlerUserAgent) {
return userAgent.toLowerCase().indexOf(crawlerUserAgent.toLowerCase()) !== -1;
});
if (knownBot) { isRequestingPrerenderedPage = true; }
//if it is BufferBot...show prerendered page
if (bufferAgent) { isRequestingPrerenderedPage = true; }
//if it is a bot and is requesting a resource...dont prerender
var resource = internals.extensionsToIgnore.some(function (extension) {
var regexp = new RegExp(extension + '$');
return regexp.test(req.url.pathname);
});
if (resource) { return false; }
return isRequestingPrerenderedPage;
};
//
// Public API
//
exports.register = function (server, options, next) {
var settings = Hoek.applyToDefaults({
serviceUrl: process.env.PRERENDER_SERVICE_URL || 'http://service.prerender.io/',
token: process.env.PRERENDER_TOKEN,
protocol: false,
beforeRender: function (req, done) { done(); },
afterRender: function (req, resp) {}
}, options);
function buildApiUrl(req) {
var prerenderUrl = settings.serviceUrl;
var forwardSlash = prerenderUrl.indexOf('/', prerenderUrl.length - 1) !== -1 ? '' : '/';
// Here we need to look at the request's protocol, not sure if this is
// correct...
var protocol = req.server.info.protocol;
//if (req.get('CF-Visitor')) {
// var match = req.get('CF-Visitor').match(/"scheme":"(http|https)"/);
// if (match) protocol = match[1];
//}
//if (req.get('X-Forwarded-Proto')) {
// protocol = req.get('X-Forwarded-Proto').split(',')[0];
//}
if (settings.protocol) {
protocol = settings.protocol;
}
var fullUrl = protocol + "://" + req.headers.host + Url.format(req.url);
return prerenderUrl + forwardSlash + fullUrl;
}
function plainResponse(resp, cb) {
var content = '';
resp.on('data', function (chunk) {
content += chunk;
});
resp.on('end', function () {
resp.body = content;
cb(null, resp);
});
}
function gzipResponse(resp, cb) {
var gunzip = Zlib.createGunzip();
var content = '';
gunzip.on('data', function (chunk) {
content += chunk;
});
gunzip.on('end', function () {
resp.body = content;
delete resp.headers['content-encoding'];
delete resp.headers['content-length'];
cb(null, resp);
});
resp.pipe(gunzip);
}
function getPrerenderedPageResponse(req, cb) {
var reqOptions = {
uri: Url.parse(buildApiUrl(req)),
followRedirect: false
};
if (settings.token) {
reqOptions.headers = {
'X-Prerender-Token': settings.token,
'User-Agent': req.headers['user-agent'],
'Accept-Encoding': 'gzip'
};
}
Request(reqOptions)
.on('error', function (err) {
cb(err);
})
.on('response', function (resp) {
var encoding = resp.headers['content-encoding'];
if (encoding && encoding === 'gzip') {
gzipResponse(resp, cb);
} else {
plainResponse(resp, cb);
}
});
}
server.ext('onRequest', function (req, reply) {
// Only handle requests with _escaped_fragment_ query param.
if (!internals.shouldShowPrerenderedPage(req)) { return reply.continue(); }
function sendResponse(resp) {
var r = reply(resp.body);
r.code(resp.statusCode);
Object.getOwnPropertyNames(resp.headers).forEach(function (k) {
r.header(k, resp.headers[k]);
});
}
settings.beforeRender(req, function (err, cached) {
if (!err && cached && typeof cached.body === 'string') {
return sendResponse(cached);
}
getPrerenderedPageResponse(req, function (err, resp) {
if (err) {
console.error('Error getting prerendered page.');
console.error(err);
console.error('Falling back to unrendered (normal) reponse...');
return reply.continue();
}
var prerenderedResponse = {
statusCode: resp.statusCode,
headers: resp.headers,
body: resp.body
};
settings.afterRender(req, prerenderedResponse);
sendResponse(prerenderedResponse);
});
});
});
next();
};
exports.register.attributes = {
pkg: require('./package.json')
};