Handling Routing in Functions
Handle custom routing within Edge Functions.
Usually, an Edge Function is written to perform a single action (e.g. write a record to the database). However, if your app's logic is split into multiple Edge Functions, requests to each action may seem slower.
Each Edge Function needs to be booted before serving a request (known as cold starts). If an action is performed less frequently (e.g. deleting a record), there is a high chance of that function experiencing a cold start.
One way to reduce cold starts and increase performance is to combine multiple actions into a single Edge Function. This way only one instance needs to be booted and it can handle multiple requests to different actions.
This allows you to:
- Reduce cold starts by combining multiple actions into one function
- Build complete REST APIs in a single function
- Improve performance by keeping one instance warm for multiple endpoints
For example, we can use a single Edge Function to create a typical CRUD API (create, read, update, delete records).
To combine multiple endpoints into a single Edge Function, you can use web application frameworks such as Express, Oak, or Hono.
Basic routing example
Here's a simple hello world example using some popular web frameworks:
1234567891011121314import { Hono } from 'jsr:@hono/hono'const app = new Hono()app.post('/hello-world', async (c) => { const { name } = await c.req.json() return new Response(`Hello ${name}!`)})app.get('/hello-world', (c) => { return new Response('Hello World!')})Deno.serve(app.fetch)
Within Edge Functions, paths should always be prefixed with the function name (in this case hello-world
).
Using route parameters
You can use route parameters to capture values at specific URL segments (e.g. /tasks/:taskId/notes/:noteId
).
Keep in mind paths must be prefixed by function name. Route parameters can only be used after the function name prefix.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485interface { : string : string}let : [] = []const = new <string, (: ) => <>>()async function (): <> { return new (.())}async function (: string): <> { const = .(() => . === ) if () { return new (.()) } else { return new ('Task not found', { : 404 }) }}async function (: ): <> { const = .().(36).(7) const = { , : '' } .() return new (.(), { : 201 })}async function (: string, : ): <> { const = .(() => . === ) if ( !== -1) { [] = { ...[] } return new (.([])) } else { return new ('Task not found', { : 404 }) }}async function (: string): <> { const = .(() => . === ) if ( !== -1) { .(, 1) return new ('Task deleted successfully') } else { return new ('Task not found', { : 404 }) }}.(async () => { const = new (.) const = . // Extract the last part of the path as the command const = ..('/').() // Assuming the last part of the path is the task ID const = try { switch () { case 'GET': if () { return () } else { return () } case 'POST': return () case 'PUT': if () { return (, ) } else { return new ('Bad Request', { : 400 }) } case 'DELETE': if () { return () } else { return new ('Bad Request', { : 400 }) } default: return new ('Method Not Allowed', { : 405 }) } } catch () { return new (`Internal Server Error: ${}`, { : 500 }) }})
URL Patterns API
If you prefer not to use a web framework, you can directly use URL Pattern API within your Edge Functions to implement routing.
This works well for small apps with only a couple of routes:
1234567891011121314151617181920212223242526272829// ... // For more details on URLPattern, check https://developer.mozilla.org/en-US/docs/Web/API/URL_Pattern_API const taskPattern = new URLPattern({ pathname: '/restful-tasks/:id' }) const matchingPath = taskPattern.exec(url) const id = matchingPath ? matchingPath.pathname.groups.id : null let task = null if (method === 'POST' || method === 'PUT') { const body = await req.json() task = body.task } // call relevant method based on method and id switch (true) { case id && method === 'GET': return getTask(supabaseClient, id as string) case id && method === 'PUT': return updateTask(supabaseClient, id as string, task) case id && method === 'DELETE': return deleteTask(supabaseClient, id as string) case method === 'POST': return createTask(supabaseClient, task) case method === 'GET': return getAllTasks(supabaseClient) default: return getAllTasks(supabaseClient) // ...