emptygif
语言: 简体中文

所有语言和框架

Fastly Compute (JavaScript) JavaScript

一个用于 Fastly Compute (JavaScript) 的 JavaScript 代码片段,以空 GIF 响应对 /pixel.gif 的请求,GIF 在启动时只解码一次。

Fastly Compute (JavaScript) JavaScript

安装
npm i @fastly/js-compute
文件
index.js
运行
fastly compute serve
/// <reference types="@fastly/js-compute" />
const GIF = Uint8Array.from(atob('R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=='), (c) => c.charCodeAt(0));

addEventListener('fetch', (event) => event.respondWith(handleRequest(event)));

function handleRequest(event) {
  if (new URL(event.request.url).pathname !== '/pixel.gif') {
    return new Response('Not Found', { status: 404 });
  }
  return new Response(GIF, {
    headers: { 'Content-Type': 'image/gif', 'Cache-Control': 'no-store' },
  });
}

我们运行了此代码片段,并比对了它返回的字节。

验证

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

官方网站: Fastly Compute (JavaScript)

更多 JavaScript 示例