Files
supabase-postgres-best-prac…/skills/supabase/references/edge-pat-routing.md

1.4 KiB

title, tags
title tags
Routing and Multi-Route Functions edge-functions, routing, hono, url-pattern

Routing and Multi-Route Functions

Handle multiple routes in a single Edge Function to minimize cold starts. Use Hono framework for clean routing. Remember to set basePath matching the function name.

Incorrect:

// Many separate functions = many cold starts
// supabase/functions/create-user/index.ts
// supabase/functions/get-user/index.ts
// supabase/functions/update-user/index.ts

// Also wrong: missing basePath causes route mismatches
import { Hono } from "jsr:@hono/hono";
const app = new Hono();
app.get("/users", handler); // Won't match /functions/v1/api/users

Correct:

import { Hono } from "jsr:@hono/hono";
import { cors } from "jsr:@hono/hono/cors";

// Single function handling all user operations
const app = new Hono().basePath("/api"); // Must match function name!

app.use("*", cors());

app.get("/users", (c) => c.json({ users: [] }));
app.get("/users/:id", (c) => c.json({ id: c.req.param("id") }));
app.post("/users", async (c) => c.json(await c.req.json(), 201));
app.delete("/users/:id", (c) => c.json({ deleted: c.req.param("id") }));
app.all("*", (c) => c.json({ error: "Not found" }, 404));

Deno.serve(app.fetch);

Invoke as https://PROJECT.supabase.co/functions/v1/api/users/123.

Reference: Routing Guide