Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.