本文へ移動

Supabase Edge Functions ​

Supabase は Firebase のオープンソースの代替であり、データベース、認証、ストレージ、そしてサーバーレス関数など、Firebase に似た一連のツールを提供します。

Supabase Edge Functions は、世界各地に分散されたサーバー側の TypeScript 関数で、ユーザーに近い場所で動作することにより性能を高めます。Deno で開発され、セキュリティの向上やモダンな JavaScript/TypeScript ランタイムなどの利点があります。

Supabase Edge Functions の使い方を説明します。

1. セットアップ ​

前提条件 ​

始める前に、Supabase CLI がインストールされていることを確認してください。未インストールの場合は、公式ドキュメントの手順に従ってください。

新しいプロジェクトの作成 ​

  1. ターミナルまたはコマンドプロンプトを開きます。

  2. 次のコマンドを実行し、ローカルマシンのディレクトリに新しい Supabase プロジェクトを作成します。

bash
supabase init

このコマンドは、現在のディレクトリで新しい Supabase プロジェクトを初期化します。

Edge Function の追加 ​

  1. Supabase プロジェクト内に、hello-world という名前の新しい Edge Function を作成します。
bash
supabase functions new hello-world

このコマンドは、指定した名前の新しい Edge Function をプロジェクト内に作成します。

2. Hello World ​

supabase/functions/hello-world/index.ts を変更して、hello-world 関数を編集します。

ts
import { Hono } from 'jsr:@hono/hono'

// change this to your function name
const functionName = 'hello-world'
const app = new Hono().basePath(`/${functionName}`)

app.get('/hello', (c) => c.text('Hello from hono-server!'))

Deno.serve(app.fetch)

3. 実行 ​

関数をローカルで実行するには、次のコマンドを使います。

  1. 次のコマンドで関数のサービスを起動します。
bash
supabase start # start the supabase stack
supabase functions serve --no-verify-jwt # start the Functions watcher

--no-verify-jwt フラグを指定すると、ローカル開発中に JWT 検証を省略できます。

  1. cURL または Postman で http://127.0.0.1:54321/functions/v1/hello-world/hello に GET リクエストを送ります。
bash
curl  --location  'http://127.0.0.1:54321/functions/v1/hello-world/hello'

このリクエストは「Hello from hono-server!」というテキストを返すはずです。

4. デプロイ ​

1 つのコマンドで、Supabase のすべての Edge Functions をデプロイできます。

bash
supabase functions deploy

また、デプロイコマンドに関数名を指定し、個別の Edge Function をデプロイすることもできます。

bash
supabase functions deploy hello-world

他のデプロイ方法については、Supabase ドキュメントの本番環境へのデプロイを参照してください。

MIT ライセンスで公開されています。