emptygif
Language: English

All languages and frameworks

AWS Lambda@Edge (CloudFront) JavaScript

A JavaScript snippet for AWS Lambda@Edge (CloudFront) that answers requests for /pixel.gif with the empty GIF, decoded once at startup.

AWS Lambda@Edge (CloudFront) JavaScript

File
index.js
// 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',
  };
};

We ran this snippet and compared the bytes it returned.

Check it

curl -sI http://localhost:8080/pixel.gif

Official site: AWS Lambda@Edge (CloudFront)

More in JavaScript