emptygif
Language: English

All languages and frameworks

nginx JavaScript (njs) JavaScript

A JavaScript snippet for nginx JavaScript (njs) that answers requests for /pixel.gif with the empty GIF, decoded once at startup.

nginx JavaScript (njs) JavaScript

File
pixel.js
Run
nginx -s reload
// pixel.js - loaded by nginx.conf:
//
//   load_module modules/ngx_http_js_module.so;
//   http {
//       js_import pixel.js;
//       server {
//           listen 8080;
//           location = /pixel.gif { js_content pixel.serve; }
//       }
//   }
const GIF = Buffer.from('R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==', 'base64');

function serve(r) {
  r.headersOut['Content-Type'] = 'image/gif';
  r.headersOut['Cache-Control'] = 'no-store';
  r.return(200, GIF);
}

export default { serve };

We ran this snippet and compared the bytes it returned.

Check it

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

Official site: nginx JavaScript (njs)

More in JavaScript