本文へ移動
章と記事の一覧

動かす基礎

Workers の実行モデル

isolate、状態を持たない、制限、課金

エディタへ移動

// isolate が再利用される間だけ残る(リクエスト間の永続性は保証されない)
// Survives only while the isolate is reused (not guaranteed across requests)
let hits = 0;

export default {
  async fetch(request, env, ctx) {
    hits += 1;

    console.log("hits =", hits);
    console.log("method =", request.method);

    // waitUntil: レスポンスを返したあとも非同期処理を継続する
    // waitUntil: Continue background tasks after returning the response
    ctx.waitUntil(Promise.resolve().then(() => console.log("post-response task complete")));

    return Response.json({
      hits,
      url: request.url,
      // エディタでは null(エディタが送るリクエストにはヘッダーがない)
      // null in the editor (the request the editor sends has no headers)
      userAgent: request.headers.get("user-agent"),
    });
  },
};

実行結果

実行するとここに結果が出ます。ネットワークは塞がれています。