-
Notifications
You must be signed in to change notification settings - Fork 25
/
index.js
168 lines (155 loc) · 3.41 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
/**
* Basic settings
*/
// The API key visible on your Prerender dashboard.
const API_KEY = 'xxx';
// The domains that you want to be prerendered.
const PRERENDERED_DOMAINS = [
'example.com'
];
/**
* Advanced settings
*/
// These are the user agents that the worker will look for to
// initiate prerendering of the site.
const BOT_AGENTS = [
'googlebot',
'yahoo! slurp',
'bingbot',
'yandex',
'baiduspider',
'facebookexternalhit',
'twitterbot',
'rogerbot',
'linkedinbot',
'embedly',
'quora link preview',
'showyoubot',
'outbrain',
'pinterest/0.',
'developers.google.com/+/web/snippet',
'slackbot',
'vkshare',
'w3c_validator',
'redditbot',
'applebot',
'whatsapp',
'flipboard',
'tumblr',
'bitlybot',
'skypeuripreview',
'nuzzel',
'discordbot',
'google page speed',
'qwantify',
'pinterestbot',
'bitrix link preview',
'xing-contenttabreceiver',
'chrome-lighthouse',
'telegrambot',
'google-inspectiontool'
];
// These are the extensions that the worker will skip prerendering
// even if any other conditions pass.
const IGNORE_EXTENSIONS = [
'.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',
'.woff',
'.ttf',
'.svg',
'.webmanifest'
];
/**
* This attaches the event listener that gets invoked when CloudFlare receives
* a request.
*/
addEventListener('fetch', event => {
const { request } = event;
const url = new URL(request.url);
const { hostname } = url;
const requestUserAgent = (request.headers.get('User-Agent') || '').toLowerCase();
const xPrerender = request.headers.get('X-Prerender');
const pathName = url.pathname.toLowerCase();
const ext = pathName.substring(pathName.lastIndexOf('.') || pathName.length);
if (
!xPrerender
&& containsOneOfThem(BOT_AGENTS, requestUserAgent)
&& !isOneOfThem(IGNORE_EXTENSIONS, ext)
&& isOneOfThem(PRERENDERED_DOMAINS, hostname)
) {
event.respondWith(prerenderRequest(request))
}
})
/**
* Helper function to check if an array contains an exact match for an element or not.
*
* @param {string[]} array - The array to check.
* @param {string} element - The element to check if the array contains.
* @returns {boolean}
*/
function isOneOfThem(array, element) {
return array.some(e => e === element);
}
/**
* Helper function to check if an array contains an element or not.
*
* @param {string[]} array - The array to check.
* @param {string} element - The element to check if the array contains.
* @returns {boolean}
*/
function containsOneOfThem(array, element) {
return array.some(e => element.indexOf(e) !== -1);
}
/**
* Function to request the prerendered version of a request.
*
* @param {Request} request - The request received by CloudFlare
* @returns {Promise<Response>}
*/
function prerenderRequest(request) {
const { url, headers } = request;
const prerenderUrl = `https://service.prerender.io/${url}`;
const headersToSend = new Headers(headers);
headersToSend.set('X-Prerender-Token', API_KEY);
const prerenderRequest = new Request(prerenderUrl, {
headers: headersToSend,
redirect: 'manual',
});
return fetch(prerenderRequest);
}