Skip to content

Commit

Permalink
fix(functions): set header "Access-Control-Allow-Methods" to "*"
Browse files Browse the repository at this point in the history
  • Loading branch information
remarkablemark committed May 30, 2023
1 parent a225c90 commit dd7e776
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
5 changes: 5 additions & 0 deletions functions/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export enum CORS_HEADERS {
ACCESS_CONTROL_ALLOW_HEADERS = 'Access-Control-Allow-Headers',
ACCESS_CONTROL_ALLOW_METHODS = 'Access-Control-Allow-Methods',
ACCESS_CONTROL_ALLOW_ORIGIN = 'Access-Control-Allow-Origin',
}
14 changes: 9 additions & 5 deletions functions/v1.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { CORS_HEADERS } from './constants';

/**
* @see {@link https://developers.cloudflare.com/workers/examples/cors-header-proxy/}
*/
Expand All @@ -16,7 +18,9 @@ export const onRequest: PagesFunction = async (context) => {
response = new Response(response.body, response);

// Set CORS headers
response.headers.set('Access-Control-Allow-Origin', '*');
Object.values(CORS_HEADERS).forEach((header) => {
response.headers.set(header, '*');
});

// Append to/Add Vary header so browser will cache response correctly
response.headers.append('Vary', 'Origin');
Expand All @@ -29,9 +33,9 @@ export const onRequest: PagesFunction = async (context) => {
*/
export const onRequestOptions: PagesFunction = async () => {
return new Response(null, {
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': '*',
},
headers: Object.values(CORS_HEADERS).reduce((accumulator, currentValue) => {
accumulator[currentValue] = '*';
return accumulator;
}, {}),
});
};

0 comments on commit dd7e776

Please sign in to comment.