无法从AWS Lambda @ Edge检索正确的uri

时间:2019-06-12 15:30:07

标签: node.js amazon-web-services aws-lambda amazon-cloudfront

我想从我的lambda @ edge函数中检索当前URI,但是event.Records[0].cf.request对象太奇怪了。

当我从如下卷曲中触发我的lambda时: curl -H 'user-agent: googlebot' https://example.com/en/test

然后我console.log无法检索/en/test的事件对象。

另一个奇怪的是,我无法在事件对象中看到标头“ user-agent:googlebot”。

基础结构:

Cloudfront->(查看者请求)lambda edge(console.log(event))-> S3存储桶(通过VueJS网站)

Lambda @ edge:

// viewer-request
exports.handler = (event, context, callback) => {
  console.log(JSON.stringify(event));
  const request = event.Records[0].cf.request;
  const headers = request.headers;
  const user_agent = headers['user-agent'];
  const host = headers['host'];
  if (user_agent && host) {
    let prerender = /googlebot|adsbot\-google|Feedfetcher\-Google|bingbot|yandex|baiduspider|Facebot|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|quora link preview|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Validator/i.test(user_agent[0].value);
    prerender = prerender || /_escaped_fragment_/.test(request.querystring);
    prerender = prerender && ! /\.(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|ttf|woff|svg|eot)$/i.test(request.uri);
    if (prerender) {
      headers['x-prerender-token'] = [{ key: 'X-Prerender-Token', value: 'token'}];
      headers['x-prerender-host'] = [{ key: 'X-Prerender-Host', value: host[0].value}];
      headers['x-prerender-cachebuster'] = [{ key: 'X-Prerender-Cachebuster', value: Date.now().toString()}];
    }
  }
  callback(null, request);
};

console.log(JSON.stringify(event))

{
    "Records": [
        {
            "cf": {
                "config": {
                    "distributionDomainName": "d1orz1ymuhldsb.cloudfront.net",
                    "distributionId": "E1L0DVYCFFDTIV",
                    "eventType": "viewer-request",
                    "requestId": "ZCPLDtSJV3gPzHbBaX6_eoX8tn2QTJi3xeOvoQwx3uuGBMqd4UBNNA=="
                },
                "request": {
                    "clientIp": "3.86.71.225",
                    "headers": {
                        "host": [
                            {
                                "key": "Host",
                                "value": "wefloc.fr"
                            }
                        ],
                        "user-agent": [
                            {
                                "key": "User-Agent",
                                "value": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/61.0.3163.59 Safari/537.36 Prerender (+https://github.com/prerender/prerender)"
                            }
                        ],
                        "x-prerender": [
                            {
                                "key": "x-prerender",
                                "value": "1"
                            }
                        ],
                        "accept": [
                            {
                                "key": "accept",
                                "value": "*/*"
                            }
                        ],
                        "referer": [
                            {
                                "key": "referer",
                                "value": "https://example.com/en/test/en/test"
                            }
                        ],
                        "accept-encoding": [
                            {
                                "key": "accept-encoding",
                                "value": "gzip, deflate"
                            }
                        ]
                    },
                    "method": "GET",
                    "querystring": "",
                    "uri": "/js/app.a6f15308.js" <=== ? should be /en/test
                }
            }
        }
    ]
}

0 个答案:

没有答案