mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
## Problem The CLI reads `supabase/config.jsonc` (and `config.json`) since v3.1.0, but every docs page still shows `config.toml` only. There's also no page explaining the formats, how to switch, the `$schema` / `#:schema` declaration, or editor setup, and no reference pages for the new `supabase config convert` and `supabase config validate` commands. Linear: CLI-2871 ## Solution - **Dual-format examples from one source.** A new directive (`features/directives/ConfigFormats.ts`) picks up any ```` ```toml ```` fence with `name=supabase/config.toml` and renders it as `supabase/config.toml` / `supabase/config.jsonc` tabs. The JSONC is generated at build time with `convertCliConfigTomlToJsonc` from `@supabase/config`, the same converter `supabase config convert` uses, so authors only write TOML and the JSONC matches what the CLI would produce (comments included). Inside `<$CodeTabs>` the JSONC is added as an extra tab. - **The format choice sticks.** `ConfigCodeTabs` keeps the selected format in `localStorage`, so choosing JSONC switches every config example on the page and on later pages. - **Config reference.** The `usage` examples on `/guides/local-development/cli/config` use the same tabs (they were highlighted as Python before), and the intro mentions `config.jsonc`. The `remotes.<branch_name>` example now uses `[remotes.staging]`, since `<branch_name>` isn't a valid TOML key. - **New guide, "Config file formats"** (`/guides/local-development/cli/config-formats`): which file wins, minimum CLI version, `convert` and `validate`, schema declaration and versioning, and editor setup (VS Code trusted domains, Even Better TOML, Tombi, SchemaStore). - **CLI reference.** Adds `supabase-config-convert` and `supabase-config-validate` to the CLI reference nav. `cli_v1_commands.yaml` is the generated v3.2.0 reference, identical to the release bot's update in #53463, so the two PRs don't conflict. - **Content.** About 60 existing config examples across the guides, including the Branching configuration page, are tagged `name=supabase/config.toml`. Branching reads `config.json` and `config.jsonc` as well, and the Branching configuration page now says so. Non-Supabase TOML (`pyproject.toml`, `bunfig.toml`) and the v2-era `[inbucket]` before/after snippet stay TOML-only. - **Format-neutral prose.** Sentences that described the config as `config.toml` now say "your config file", naming `supabase/config.toml` and `supabase/config.jsonc` once per page. TOML section syntax in prose (`max_frequency` under `[auth.email]`) is written as key paths (`auth.email.max_frequency`), which read the same in both formats. Headings that named `config.toml` keep their old anchors. Sentences about what `supabase init` creates still say `config.toml`. - **Markdown export for agents.** `internals/generate-guides-markdown.ts` runs the same transform, so the `.md` pages served to AI agents include the JSONC example after each TOML one. - A test converts every tagged example under `content/`, so a broken TOML example fails tests instead of the build. New dependencies for `apps/docs`: `@supabase/config` (^1.8.0) and `effect` (the same `effect` version Studio pins). `config convert` and `config validate` ship in CLI v3.2.0, which is now stable. ## Review instructions 1. Open `/docs/guides/local-development/seeding-your-database` on the preview. Switch an example to `supabase/config.jsonc` and check the other examples on the page switch too. 2. Open `/docs/guides/local-development/cli/config` and check the usage examples open on JSONC. 3. Read `/docs/guides/local-development/cli/config-formats`. 4. Open `/docs/reference/cli/supabase-config-convert` and `/docs/reference/cli/supabase-config-validate`. ## Self-review Ran the `review-the-docs` local self-review (content, pipeline, and component checks) over the 54 changed pages. - No blockers. Fixed the nits it found: leftover TOML section syntax in prose (diff engines, declarative schemas, Branching, the common settings table on Local and hosted projects), a few long or ungrammatical sentences, and the conversion follow-up steps on Config file formats, now a list. - Links, custom anchors, nav wiring, and frontmatter check out. - The `.md` export on the preview shows each TOML example followed by its JSONC version. - `test-the-docs` deferred: the changed fences are config snippets, and `ConfigFormats.test.ts` already converts every tagged example. No procedural steps changed apart from the `config convert` and `config validate` commands on the new page. ## Checklist Check all before review: - [x] I have read [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) - [x] If I wrote a new docs topic or edited an existing topic, I used the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs [style guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)
101 lines
3.4 KiB
Plaintext
101 lines
3.4 KiB
Plaintext
---
|
|
id: 'function-background-tasks'
|
|
title: 'Background Tasks'
|
|
description: 'How to run background tasks in an Edge Function outside of the request handler'
|
|
subtitle: 'Run background tasks in an Edge Function outside of the 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 and leave the task running in the background.
|
|
|
|
This allows you to:
|
|
|
|
- Respond to users while processing continues
|
|
- Handle async operations without blocking the response
|
|
|
|
---
|
|
|
|
## Overview
|
|
|
|
You can use `EdgeRuntime.waitUntil(promise)` to explicitly mark background tasks. The Function instance continues to run until the promise provided to `waitUntil` completes.
|
|
|
|
```ts
|
|
import { withSupabase } from 'npm:@supabase/server@^1'
|
|
|
|
// Mark the asyncLongRunningTask's returned promise as a background task.
|
|
// ⚠️ We are NOT using `await` because we don't want it to block!
|
|
EdgeRuntime.waitUntil(asyncLongRunningTask())
|
|
|
|
export default {
|
|
fetch: withSupabase({ auth: 'user' }, async (req, ctx) => {
|
|
return Response.json({ ok: true })
|
|
}),
|
|
}
|
|
```
|
|
|
|
You can call `EdgeRuntime.waitUntil` in the request handler too. This will not block the request.
|
|
|
|
```ts
|
|
import { withSupabase } from 'npm:@supabase/server@^1'
|
|
|
|
export default {
|
|
fetch: withSupabase({ auth: 'user' }, async (req, ctx) => {
|
|
// Won't block the request, runs in background.
|
|
EdgeRuntime.waitUntil(asyncLongRunningTask())
|
|
|
|
return Response.json({ ok: true })
|
|
}),
|
|
}
|
|
```
|
|
|
|
You can listen to the `beforeunload` event handler to be notified when the Function is about to be shut down.
|
|
|
|
```tsx
|
|
import { withSupabase } from 'npm:@supabase/server@^1'
|
|
|
|
EdgeRuntime.waitUntil(asyncLongRunningTask())
|
|
|
|
// Use beforeunload event handler to be notified when function is about to shutdown
|
|
addEventListener('beforeunload', (ev) => {
|
|
console.log('Function will be shutdown due to', ev.detail?.reason)
|
|
// Save state or log the current progress
|
|
})
|
|
|
|
export default {
|
|
fetch: withSupabase({ auth: 'user' }, async (req, ctx) => {
|
|
return Response.json({ ok: true })
|
|
}),
|
|
}
|
|
```
|
|
|
|
## Handling errors
|
|
|
|
We recommend using `try`/`catch` blocks within your background task function to handle errors.
|
|
|
|
You can also add an event listener to [`unhandledrejection`](https://developer.mozilla.org/en-US/docs/Web/API/Window/unhandledrejection_event) to handle any promises without a rejection handler.
|
|
|
|
```tsx
|
|
addEventListener('unhandledrejection', (ev) => {
|
|
console.log('unhandledrejection', ev.reason)
|
|
ev.preventDefault()
|
|
})
|
|
```
|
|
|
|
<Admonition type="note">
|
|
|
|
The maximum duration is capped based on the wall-clock, CPU, and memory limits. The [function will shut down](/kb/troubleshooting/edge-function-shutdown-reasons-explained) when it reaches one of these [limits](/docs/guides/functions/limits).
|
|
|
|
</Admonition>
|
|
|
|
---
|
|
|
|
## Testing background tasks locally
|
|
|
|
When testing Edge Functions locally with [Supabase CLI](/docs/guides/local-development/cli/getting-started), 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 your config file (`supabase/config.toml` or `supabase/config.jsonc`) with the following settings:
|
|
|
|
```toml supabase/config.toml
|
|
[edge_runtime]
|
|
policy = "per_worker"
|
|
```
|