The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
Break out Suggestions into Best Practices doc
dannykng355432c3de77
diff --git a/apps/docs/components/Navigation/Navigation.constants.ts b/apps/docs/components/Navigation/Navigation.constants.tsindex 36b27dccfb6..24037a271c7 100644--- a/apps/docs/components/Navigation/Navigation.constants.ts+++ b/apps/docs/components/Navigation/Navigation.constants.ts@@ -228,6 +228,7 @@ export const menuItems: NavMenu = {{ name: 'Overview', url: '/guides/functions', items: [] },{ name: 'Quickstart', url: '/guides/functions/quickstart', items: [] },{ name: 'Auth', url: '/guides/functions/auth', items: [] },+ { name: 'Best Practices', url: '/guides/functions/best-practices', items: [] },{ name: 'Examples', url: '/guides/functions/examples', items: [] },{ name: 'CI/CD Workflow', url: '/guides/functions/cicd-workflow', items: [] },],diff --git a/apps/docs/pages/guides/functions/best-practices.mdx b/apps/docs/pages/guides/functions/best-practices.mdxnew file mode 100644index 00000000000..b670cd73a05--- /dev/null+++ b/apps/docs/pages/guides/functions/best-practices.mdx@@ -0,0 +1,64 @@+import Layout from '~/layouts/DefaultGuideLayout'++export const meta = {+ id: 'functions-best-practices',+ title: 'Edge Functions Best Practices',+ description: 'Globally distributed TypeScript functions.',+}++## Database Functions vs Edge Functions++For data-intensive operations we recommend using [Database Functions](/docs/guides/database/functions), which are executed within your database+and can be called remotely using the [REST and GraphQL API](/docs/guides/api).++For use-cases which require low-latency we recommend [Edge Functions](/docs/guides/functions), which are globally-distributed and can be written in TypeScript.++## Organizing your Edge Functions++We recommend developing “fat functions”. This means that you should develop few large functions, rather than many small functions. One common pattern when developing Functions is that you need to share code between two or more Functions. To do this, you can store any shared code in a folder prefixed with an underscore (`_`). We recommend this folder structure:++```bash+└── supabase+ ├── functions+ │ ├── _shared+ │ | ├── supabaseAdmin.ts # Supabase client with SERVICE_ROLE key+ │ │ └── supabaseClient.ts # Supabase client with ANON key+ │ ├── function-one # use hyphens to name functions+ │ │ └── index.ts+ │ └── function-two+ │ └── index.ts+ ├── migrations+ └── config.toml+```++## Naming Edge Functions++We recommend using hyphens to name functions because hyphens are the most URL-friendly of all the naming conventions (snake_case, camelCase, PascalCase).++## CORS (Cross-Origin Resource Sharing)++We recommend adding a check to handle [CORS Preflight](https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request) requests in your edge function to be able to invoke the function from browsers.++See the [example on GitHub](https://github.com/supabase/supabase/blob/master/examples/edge-functions/supabase/functions/browser-with-cors/index.ts).++```ts+export const corsHeaders = {+ 'Access-Control-Allow-Origin': '*',+ 'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey',+}++serve(async (req) => {+ if (req.method === 'OPTIONS') {+ return new Response('ok', { headers: corsHeaders })+ }+ ...+})+```++## Using HTTP Methods++Edge Functions supports `GET`, `POST`, `PUT`, `PATCH`, `DELETE`, and `OPTIONS`. A function can be designed to perform different actions based on a request's HTTP method. See the [example on building a RESTful service](https://github.com/supabase/supabase/tree/master/examples/edge-functions/supabase/functions/restful-tasks) to learn how to handle different HTTP methods in your function.++export const Page = ({ children }) => <Layout meta={meta} children={children} />++export default Page