emptygif
言語: 日本語

すべての言語・フレームワーク

Fastly Compute (JavaScript) JavaScript

/pixel.gif へのリクエストに空の GIF を返す、Fastly Compute (JavaScript) 用の JavaScript スニペット。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 の他の例