AWS Lambda@Edge (CloudFront) JavaScript
適用於 AWS Lambda@Edge (CloudFront) 的 JavaScript 程式碼片段,以空 GIF 回應對 /pixel.gif 的請求,GIF 在啟動時只解碼一次。
AWS Lambda@Edge (CloudFront) JavaScript
// Viewer-request trigger: answer /pixel.gif at the edge, pass everything else through.
const GIF_BASE64 = 'R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==';
exports.handler = async (event) => {
const request = event.Records[0].cf.request;
if (request.uri !== '/pixel.gif') return request;
return {
status: '200',
statusDescription: 'OK',
headers: {
'content-type': [{ key: 'Content-Type', value: 'image/gif' }],
'cache-control': [{ key: 'Cache-Control', value: 'no-store' }],
},
body: GIF_BASE64,
bodyEncoding: 'base64',
};
};
我們執行了這段程式碼,並比對了它回傳的位元組。
驗證
curl -sI http://localhost:8080/pixel.gif
官方網站: AWS Lambda@Edge (CloudFront)