Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save frankfuu/0400e6db10b6b5227861a07d2f2984f3 to your computer and use it in GitHub Desktop.
Save frankfuu/0400e6db10b6b5227861a07d2f2984f3 to your computer and use it in GitHub Desktop.
cloudfront-set-useragent-for-crawlers
'use strict';
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
const headers = request.headers;
const uri = request['uri'];
const botPattern = "Googlebot\\/|Googlebot-Mobile|Googlebot-Image|Googlebot-News|Googlebot-Video|AdsBot-Google([^-]|$)|AdsBot-Google-Mobile|Mediapartners-Google|Mediapartners \\(Googlebot\\)|APIs-Google|bingbot";
var re = new RegExp(botPattern, 'i');
var userAgent = headers['user-agent'][0]['value'];
if (re.test(userAgent)) {
headers['user-agent'] = [{key: 'User-Agent', value: userAgent}];
} else {
headers['user-agent'] = [{key: 'User-Agent', value: 'Human'}];
}
callback(null, request);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment