Skip to content

Commit a0b946f

Browse files
authored
docs: update cors documentation to include new method (supabase#42644)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Documentation** * Updated Edge Functions CORS header guides with version-specific approaches for SDK v2.95.0+ and earlier versions. * Revised code examples across multiple Edge Functions to reflect current CORS header implementation patterns. * Enhanced troubleshooting documentation with updated preflight handling guidance. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent cc7eab9 commit a0b946f

9 files changed

Lines changed: 108 additions & 35 deletions

File tree

apps/docs/content/guides/functions/cors.mdx

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,16 @@ See the [example on GitHub](https://github.com/supabase/supabase/blob/master/exa
1010

1111
### Recommended setup
1212

13-
We recommend adding a `cors.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:
13+
<Admonition type="tip">
1414

15-
```ts cors.ts
16-
export const corsHeaders = {
17-
'Access-Control-Allow-Origin': '*',
18-
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
19-
}
20-
```
15+
**For `@supabase/supabase-js` v2.95.0 and later:** Import CORS headers directly from the SDK to ensure they stay synchronized with any new headers added to the client libraries.
16+
17+
</Admonition>
2118

22-
You can then import and use the CORS headers within your functions:
19+
Import `corsHeaders` from `@supabase/supabase-js/cors` to automatically get all required headers:
2320

2421
```ts index.ts
25-
import { corsHeaders } from '../_shared/cors.ts'
22+
import { corsHeaders } from '@supabase/supabase-js/cors'
2623

2724
console.log(`Function "browser-with-cors" up and running!`)
2825

@@ -50,3 +47,24 @@ Deno.serve(async (req) => {
5047
}
5148
})
5249
```
50+
51+
This approach ensures that when new headers are added to the Supabase SDK, your Edge Functions automatically include them, preventing CORS errors.
52+
53+
#### For versions before 2.95.0
54+
55+
If you're using `@supabase/supabase-js` before v2.95.0, you'll need to hardcode the CORS headers. Add a `cors.ts` file within a [`_shared` folder](/docs/guides/functions/quickstart#organizing-your-edge-functions):
56+
57+
```ts _shared/cors.ts
58+
export const corsHeaders = {
59+
'Access-Control-Allow-Origin': '*',
60+
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
61+
}
62+
```
63+
64+
Then import it in your function:
65+
66+
```ts index.ts
67+
import { corsHeaders } from '../_shared/cors.ts'
68+
69+
// ... rest of your function code
70+
```

apps/docs/content/guides/functions/examples/cloudflare-turnstile.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ supabase functions new cloudflare-turnstile
2323
And add the code to the `index.ts` file:
2424

2525
```ts index.ts
26-
import { corsHeaders } from '../_shared/cors.ts'
26+
import { corsHeaders } from '@supabase/supabase-js/cors' // v2.95.0+
2727
2828
console.log('Hello from Cloudflare Trunstile!')
2929

apps/docs/content/troubleshooting/unable-to-call-edge-function.mdx

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,58 @@ If you're having trouble invoking an Edge Function or experiencing CORS issues,
1414

1515
## Proper CORS handling
1616

17-
Make sure your function handles OPTIONS preflight requests:
17+
Make sure your function handles OPTIONS preflight requests.
18+
19+
### Recommended (v2.95.0+)
20+
21+
<Admonition type="tip">
22+
23+
**For `@supabase/supabase-js` v2.95.0 and later:** Import CORS headers directly from the SDK to ensure they stay synchronized with any new headers added to the client libraries.
24+
25+
</Admonition>
1826

1927
```tsx
28+
// Recommended approach
29+
import { corsHeaders } from '@supabase/supabase-js/cors' // v2.95.0+
30+
31+
Deno.serve(async (req) => {
32+
if (req.method === 'OPTIONS') {
33+
return new Response('ok', { headers: corsHeaders })
34+
}
35+
36+
// Your function logic here
37+
return new Response(JSON.stringify({ status: 'Success' }), {
38+
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
39+
})
40+
})
41+
```
42+
43+
### For versions before 2.95.0
44+
45+
If you're using `@supabase/supabase-js` before v2.95.0, you'll need to hardcode the CORS headers. Add a `cors.ts` file within a [`_shared` folder](/docs/guides/functions/quickstart#organizing-your-edge-functions):
46+
47+
```ts
48+
// _shared/cors.ts
49+
export const corsHeaders = {
50+
'Access-Control-Allow-Origin': '*',
51+
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
52+
}
53+
```
54+
55+
Then import it in your function:
56+
57+
```tsx
58+
import { corsHeaders } from '../_shared/cors.ts'
59+
2060
Deno.serve(async (req) => {
2161
// Handle CORS preflight requests
2262
if (req.method === 'OPTIONS') {
23-
return new Response(null, {
24-
status: 200,
25-
headers: {
26-
'Access-Control-Allow-Origin': '*',
27-
'Access-Control-Allow-Methods': 'POST, GET, OPTIONS',
28-
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
29-
},
30-
})
63+
return new Response('ok', { headers: corsHeaders })
3164
}
3265

3366
// Your function logic here
34-
return new Response('Success', {
35-
headers: { 'Access-Control-Allow-Origin': '*' },
67+
return new Response(JSON.stringify({ status: 'Success' }), {
68+
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
3669
})
3770
})
3871
```

examples/edge-functions/supabase/functions/_shared/cors.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
// NOTE: For @supabase/supabase-js v2.95.0 and later, you can import CORS headers
2+
// directly from the SDK instead of hardcoding them:
3+
//
4+
// import { corsHeaders } from 'jsr:@supabase/supabase-js@2/cors'
5+
//
6+
// This ensures your CORS headers stay synchronized with any new headers added to the SDK.
7+
// For versions before 2.95.0, use this hardcoded configuration:
8+
19
export const corsHeaders = {
210
'Access-Control-Allow-Origin': '*',
311
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',

examples/edge-functions/supabase/functions/browser-with-cors/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
// https://deno.land/manual/getting_started/setup_your_environment
33
// This enables autocomplete, go to definition, etc.
44

5-
import { corsHeaders } from '../_shared/cors.ts'
5+
// For @supabase/supabase-js v2.95.0+, import CORS headers directly from the SDK:
6+
import { corsHeaders } from 'jsr:@supabase/supabase-js@2/cors'
7+
8+
// For older versions, use a shared cors.ts file:
9+
// import { corsHeaders } from '../_shared/cors.ts'
610

711
console.log(`Function "browser-with-cors" up and running!`)
812

examples/edge-functions/supabase/functions/get-tshirt-competition/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
// This enables autocomplete, go to definition, etc.
44

55
import { createClient } from 'npm:supabase-js@2'
6-
import { corsHeaders } from '../_shared/cors.ts'
6+
// New approach (v2.95.0+)
7+
import { corsHeaders } from 'jsr:@supabase/supabase-js@2/cors'
8+
// For older versions:
9+
// import { corsHeaders } from '../_shared/cors.ts'
710

811
console.log(`Function "get-tshirt-competition" up and running!`)
912

examples/edge-functions/supabase/functions/read-storage/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
// This enables autocomplete, go to definition, etc.
44

55
import { createClient } from 'npm:supabase-js@2'
6-
7-
const corsHeaders = {
8-
'Access-Control-Allow-Origin': '*',
9-
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey',
10-
}
6+
// New approach (v2.95.0+)
7+
import { corsHeaders } from 'jsr:@supabase/supabase-js@2/cors'
8+
// For older versions, use hardcoded headers:
9+
// const corsHeaders = {
10+
// 'Access-Control-Allow-Origin': '*',
11+
// 'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
12+
// }
1113

1214
Deno.serve(async (req) => {
1315
// read a text file from storage and print its contents

examples/edge-functions/supabase/functions/restful-tasks/index.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
// This enables autocomplete, go to definition, etc.
44

55
import { createClient, SupabaseClient } from 'npm:supabase-js@2'
6-
7-
const corsHeaders = {
8-
'Access-Control-Allow-Origin': '*',
9-
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey',
10-
'Access-Control-Allow-Methods': 'POST, GET, OPTIONS, PUT, DELETE',
11-
}
6+
// New approach (v2.95.0+)
7+
import { corsHeaders } from 'jsr:@supabase/supabase-js@2/cors'
8+
// For older versions, use hardcoded headers:
9+
// const corsHeaders = {
10+
// 'Access-Control-Allow-Origin': '*',
11+
// 'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
12+
// 'Access-Control-Allow-Methods': 'POST, GET, OPTIONS, PUT, DELETE',
13+
// }
1214

1315
interface Task {
1416
name: string

examples/edge-functions/supabase/functions/select-from-table-with-auth-rls/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
// This enables autocomplete, go to definition, etc.
44

55
import { createClient } from 'npm:supabase-js@2'
6-
import { corsHeaders } from '../_shared/cors.ts'
6+
// New approach (v2.95.0+)
7+
import { corsHeaders } from 'jsr:@supabase/supabase-js@2/cors'
8+
// For older versions:
9+
// import { corsHeaders } from '../_shared/cors.ts'
710

811
console.log(`Function "select-from-table-with-auth-rls" up and running!`)
912

0 commit comments

Comments
 (0)