边缘函数
Run TypeScript functions globally at the edge.
边缘函数是服务器端的 TypeScript 函数,全球分布在边缘——靠近你的用户。它们可以用来监听 webhook 或将你的 Supabase 项目与第三方服务(比如 Stripe)集成。边缘函数是使用 Deno 开发的,这为你作为开发者带来一些好处:
🌐 Edge Functions are server-side TypeScript functions, distributed globally at the edge—close to your users. They can be used for listening to webhooks or integrating your Supabase project with third-parties like Stripe. Edge Functions are developed using Deno, which offers a few benefits to you as a developer:
- 它是开源的。
- 它是可移植的。Supabase Edge Functions 可以在本地运行,也可以在任何其他兼容 Deno 的平台上运行(包括自托管的基础设施)。
- 它主要是 TypeScript,并支持 WASM。
- 边缘功能是全球分布的,以实现低延迟。
它是怎么运作的 #
🌐 How it works
- 请求进入边缘网关(中继)——网关负责路由流量、处理认证头/JWT 验证,以及应用路由/流量规则。
- 身份验证和策略已应用 — 网关(或你的函数)可以验证 Supabase JWT,应用速率限制,并在执行代码前集中进行安全检查。
- 边缘运行时 执行你的函数 — 函数会在离用户最近的区域分布式边缘运行时节点上运行,以实现最低延迟。
- 集成与数据访问 — 函数通常会调用 Supabase API(认证、Postgres、存储)或第三方 API。对于 Postgres,建议使用适合边缘/无服务器环境的连接策略(请参阅
connect-to-postgres指南)。 - 可观察性和日志 — 调用会生成日志和指标,你可以在仪表板或下游监控(如 Sentry 等)中查看。
- 响应通过网关返回 — 网关将响应转发回客户端并记录请求元数据。
快速技术注意 #
🌐 Quick technical notes
- 运行时: Supabase Edge Runtime(与 Deno 兼容、以 TypeScript 为主的运行时)。函数是
.ts文件,并导出一个处理器。 - 本地开发一致性: 使用 Supabase CLI 进行类似生产环境的本地运行,以加快迭代速度(
supabase functions serve命令)。 - 全球部署: 通过 Supabase 仪表板、CLI 或 MCP 部署你的 Edge Functions。
- 冷启动与并发: 可能会出现冷启动——因此设计时要考虑短时、幂等的操作。耗时较长的重任务应移至后台工作程序处理。
- 数据库连接: 把 Postgres 当作远程的、带连接池的服务 — 使用连接池或适合无服务器的驱动。
- 秘密: 将凭证存储在 Supabase 项目秘密 中,并通过环境变量访问它们。
什么时候使用 Edge 函数 #
🌐 When to use Edge Functions
- 需要低延迟的认证或公共 HTTP 端点。
- Webhook 接收器(Stripe、GitHub 等)。
- 按需生成图片或 Open Graph。
- 小型 AI 推断任务或协调调用外部 LLM API(比如 OpenAI)的操作
- 发送交易邮件。
- 为 Slack、Discord 等制作消息机器人
Get started#
示例 #
🌐 Examples
看看 GitHub 上的 Supabase Edge Function 示例 或试试这些示例:
🌐 Check out Supabase Edge Function Examples in GitHub or try these examples:
Supabase integration#
- Use the Supabase client inside your Edge Function.
With supabase-js
- Connect to Postgres from Edge Functions.
Connect to Postgres
- Combine Kysely with Deno Postgres for a convenient developer experience when interacting directly with your Postgres database.
Type-Safe SQL with Kysely
- Send CORS headers for invoking from the browser.
With CORS headers
- Learn how to use HTTP methods and paths to build a RESTful service for managing tasks.
Building a RESTful Service API
- Route requests with Oak server middleware.
Oak Server Middleware
- Stream Server-Sent Events from Edge Functions.
Web Stream
- Get user location data from user's IP address.
Get User Location
- Read a file from Supabase Storage.
Working with Supabase Storage
- Process multipart/form-data.
Upload File
Webhooks & payments#
AI & media#
- Access 100,000+ Machine Learning models.
Hugging Face
- Use OpenAI in Edge Functions.
OpenAI
- Generate images with Amazon Bedrock in Edge Functions.
Amazon Bedrock
- Generate Open Graph images with Deno and Supabase Edge Functions.
Open Graph Image Generation
- Cache generated images with Supabase Storage CDN.
OG Image Generation & Storage CDN Caching
- Generate screenshots with Puppeteer.
Puppeteer
Bots & email#
Operations & security#
- Monitor Edge Functions with the Sentry Deno SDK.
Monitoring with Sentry
- Deploy Edge Functions with GitHub Actions.
GitHub Actions
- Build an Edge Functions Counter with Upstash Redis.
Upstash Redis
- Rate-limit Edge Functions with Upstash Redis.
Rate Limiting
- Protect forms with Cloudflare Turnstile.
Cloudflare Turnstile