Skip to content

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

feat: add import maps guide.

thorwebdevd739a011f62c
diff --git a/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts b/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts
index e46dafbf7df..8fa5333d8d0 100644
--- a/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts
+++ b/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts
@@ -423,12 +423,12 @@ export const functions = {
items: [
{ name: 'Quickstart', url: '/guides/functions/quickstart', items: [] },
{ name: 'Auth', url: '/guides/functions/auth', items: [] },
- { name: 'CORS', url: '/guides/functions/cors', items: [] },
{ name: 'CI/CD Workflow', url: '/guides/functions/cicd-workflow', items: [] },
+ { name: 'CORS', url: '/guides/functions/cors', items: [] },
{ name: 'Debugging', url: '/guides/functions/debugging', items: [] },
+ { name: 'Import Maps', url: '/guides/functions/import-maps', items: [] },
{ name: 'Local Development', url: '/guides/functions/local-development', items: [] },
{ name: 'Managing Secrets', url: '/guides/functions/secrets', items: [] },
- { name: 'Tips', url: '/guides/functions/tips', items: [] },
{
name: 'Examples',
url: '/guides/functions/examples',
diff --git a/apps/docs/pages/guides/functions/cors.mdx b/apps/docs/pages/guides/functions/cors.mdx
index e991ad181a9..14f1a1208d9 100644
--- a/apps/docs/pages/guides/functions/cors.mdx
+++ b/apps/docs/pages/guides/functions/cors.mdx
@@ -12,7 +12,7 @@ See the [example on GitHub](https://github.com/supabase/supabase/blob/master/exa
### Recommended setup
-We recommend adding a `corst.ts` file within a [`_shared` folder](/docs/guides/functions/tips#organizing-your-edge-functions) which makes it easy to reuse the CORS headers across functions:
+We recommend adding a `corst.ts` file within a [`_shared` folder](/docs/guides/functions/quickstart#organizing-your-edge-functions) which makes it easy to reuse the CORS headers across functions:
```ts cors.ts
export const corsHeaders = {
diff --git a/apps/docs/pages/guides/functions/import-maps.mdx b/apps/docs/pages/guides/functions/import-maps.mdx
new file mode 100644
index 00000000000..1509a9cd8a9
--- /dev/null
+++ b/apps/docs/pages/guides/functions/import-maps.mdx
@@ -0,0 +1,53 @@
+import Layout from '~/layouts/DefaultGuideLayout'
+
+export const meta = {
+ id: 'functions-import-maps',
+ title: 'Import Maps',
+ description: 'Using Import Maps.',
+}
+
+Since [Supabase CLI](https://supabase.com/docs/guides/resources/supabase-cli) version `1.33.0` [import maps](https://deno.land/[email protected]/node/import_maps) can be used with Supabase Edge Functions.
+
+[Import maps](https://github.com/WICG/import-maps#the-import-map) is a web-platform standard that allows you to use bare specifiers with Deno without having to install the Node.js package locally.
+
+So if we want to do the following in our code:
+
+```ts, ignore
+import lodash from "lodash";
+```
+
+We can accomplish this using an import map, and we don't even have to install the `lodash` package locally. We would want to create a JSON file (for example **import_map.json**) with the following:
+
+```json
+{
+ "imports": {
+ "lodash": "https://cdn.skypack.dev/lodash"
+ }
+}
+```
+
+## Import Map Placement
+
+We recommend creating one `import_map.json` within the `/supabase/functions` folder (see [Organizing your Edge Functions](/docs/guides/functions/quickstart#organizing-your-edge-functions)), similar to a `package.json` file, to define imports that can be used across all of your project's functions.
+
+Alternatively, you can create one `import_map.json` file in each function folder, which will take priority over a top-level file.
+
+Lastly, you can override this default behaviour by providing the `--import-map <string>` flag to the `serve` and `deploy` commands.
+
+## Visual Studio Code Configuration
+
+In order for vscode to understand the imports correctly, you need to specify the `deno.importMap` flag in your `.vscode/settings.json` file:
+
+```json settings.json
+{
+ "deno.enable": true,
+ "deno.unstable": true,
+ "deno.importMap": "./supabase/functions/import_map.json"
+}
+```
+
+For a full guide on developing with Deno in Visual Studio Code, see [this guide](https://deno.land/[email protected]/vscode_deno).
+
+export const Page = ({ children }) => <Layout meta={meta} children={children} />
+
+export default Page
diff --git a/apps/docs/pages/guides/functions/local-development.mdx b/apps/docs/pages/guides/functions/local-development.mdx
index 01bd0f88a4d..fffd6ac7072 100644
--- a/apps/docs/pages/guides/functions/local-development.mdx
+++ b/apps/docs/pages/guides/functions/local-development.mdx
@@ -15,7 +15,7 @@ supabase functions serve hello-world # start the Function watcher
The `functions serve` command has hot-reloading capabilities. It will watch for any changes to your files and restart the Deno server.
-### Invoke functions locally
+### Invoking functions locally
While serving your local Function, you can invoke it using curl:
diff --git a/apps/docs/pages/guides/functions/quickstart.mdx b/apps/docs/pages/guides/functions/quickstart.mdx
index 7b489f2009e..e4e17a3a708 100644
--- a/apps/docs/pages/guides/functions/quickstart.mdx
+++ b/apps/docs/pages/guides/functions/quickstart.mdx
@@ -55,9 +55,9 @@ If you want to use Edge Functions to handle webhooks (e.g. [Stripe payment webho
</Admonition>
-## Execute remote functions
+## Invoking remote functions
-You can execute Edge Functions using curl. Copy the curl command from the Dashboard. It should look like this:
+You can invoke Edge Functions using curl:
```bash
curl --request POST 'https://<project_ref>.functions.supabase.co/hello-world' \
@@ -66,10 +66,62 @@ curl --request POST 'https://<project_ref>.functions.supabase.co/hello-world' \
--data '{ "name":"Functions" }'
```
+<Admonition type="note">
+
If you receive an error `Invalid JWT`, find the `ANON_KEY` of your project in the Dashboard under `Settings > API`.
+</Admonition>
+
+or using one of the [client libraries](/docs#reference-documentation), e.g. using [supabase-js](/docs/reference/javascript/functions-invoke):
+
+```js
+import { createClient } from '@supabase/supabase-js'
+
+// Create a single supabase client for interacting with your database
+const supabase = createClient('https://xyzcompany.supabase.co', 'public-anon-key')
+
+const { data, error } = await supabase.functions.invoke('hello-world', {
+ body: { name: 'Functions' },
+})
+```
+
After invoking your Edge Function you should see the response `{ "message":"Hello 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
+ │ ├── import_map.json # A top-level import map to use across functions.
+ │ ├── _shared
+ │ │ ├── supabaseAdmin.ts # Supabase client with SERVICE_ROLE key.
+ │ │ └── supabaseClient.ts # Supabase client with ANON key.
+ │ │ └── cors.ts # Reusable CORS headers.
+ │ ├── 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).
+
+## 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.
+
## Limitations
- Deno Deploy limitations
diff --git a/apps/docs/pages/guides/functions/tips.mdx b/apps/docs/pages/guides/functions/tips.mdx
deleted file mode 100644
index 9c96998db4a..00000000000
--- a/apps/docs/pages/guides/functions/tips.mdx
+++ /dev/null
@@ -1,45 +0,0 @@
-import Layout from '~/layouts/DefaultGuideLayout'
-
-export const meta = {
- id: 'functions-tips',
- title: 'Edge Functions Tips',
- description: 'Misc Tips for Supabase Edge 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
- │ │ └── cors.ts # Reusable CORS headers
- │ ├── 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).
-
-## 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
diff --git a/apps/www/lib/redirects.js b/apps/www/lib/redirects.js
index 1e992b67d9e..108782dcb29 100644
--- a/apps/www/lib/redirects.js
+++ b/apps/www/lib/redirects.js
@@ -1554,6 +1554,11 @@ module.exports = [
source: '/docs/guides/functions/examples',
destination: '/docs/guides/functions',
},
+ {
+ permanent: true,
+ source: '/docs/guides/functions/best-practices',
+ destination: '/docs/guides/functions/quickstart',
+ },
{
permanent: true,
source: '/projects',