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 範例