Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

fix: add a new guide on how to run background tasks in Edge Functions (#30313)

Lakshan Pererafe0e32dce537
diff --git a/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts b/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts
index afd63c99561..99b1cfd8caa 100644
--- a/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts
+++ b/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts
@@ -1250,6 +1250,10 @@ export const functions: NavMenuConstant = {
name: 'Handling Routing in Functions',
url: '/guides/functions/routing',
},
+ {
+ name: 'Background Tasks',
+ url: '/guides/functions/background-tasks',
+ },
{
name: 'Running AI Models',
url: '/guides/functions/ai-models',
diff --git a/apps/docs/content/guides/functions/background-tasks.mdx b/apps/docs/content/guides/functions/background-tasks.mdx
new file mode 100644
index 00000000000..7d685754371
--- /dev/null
+++ b/apps/docs/content/guides/functions/background-tasks.mdx
@@ -0,0 +1,50 @@
+---
+id: 'function-background-tasks'
+title: 'Background Tasks'
+description: 'How to run background tasks in an Edge Function outside of request handler'
+subtitle: 'How to run background tasks in an Edge Function outside of request handler'
+---
+
+Edge Function instances can process background tasks outside of the request handler. Background tasks are useful for asynchronous operations like uploading a file to Storage, updating a database, or sending events to a logging service. You can respond to the request immediately or leave the task running in the background.
+
+A background task can run until the Edge Function instance hits its [wall-clock limit](/docs/guides/functions/limits).
+
+Here's an example of defining a background task using a custom event.
+
+```ts
+// define a custom event
+class MyBackgroundTaskEvent extends Event {
+ readonly taskPromise: Promise<string>
+
+ constructor(taskPromise) {
+ super('myBackgroundTask')
+ this.taskPromise = taskPromise
+ }
+}
+
+globalThis.addEventListener('myBackgroundTask', async (event: MyBackgroundTaskEvent) => {
+ const res = await event.taskPromise
+ console.log(await res.json())
+})
+
+Deno.serve(async (req) => {
+ const fetchPromise = fetch('https://httpbin.org/json')
+ const event = new MyBackgroundTaskEvent(fetchPromise)
+ globalThis.dispatchEvent(event)
+
+ return new Response('ok')
+})
+```
+
+### Testing background tasks locally
+
+When testing Edge Functions locally with Supabase CLI, the instances are terminated automatically after a request is completed. This will prevent background tasks from running to completion.
+
+To prevent that, you can update the `supabase/config.toml` with the following settings:
+
+```toml
+[edge_runtime]
+policy = "per_worker"
+```
+
+When running with `per_worker` policy, Function won't auto-reload on edits. You will need to manually restart it by running `supabase functions serve`.