Created
March 5, 2019 05:46
-
-
Save frankfuu/0400e6db10b6b5227861a07d2f2984f3 to your computer and use it in GitHub Desktop.
cloudfront-set-useragent-for-crawlers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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