@@ -16,13 +16,12 @@ tags:
1616
1717``` js
1818// We support the GET, POST, HEAD, and OPTIONS methods from any origin,
19- // and accept the Content-Type header on requests. These headers must be
20- // present on all responses to all CORS requests. In practice, this means
19+ // and allow any header on requests. These headers must be present
20+ // on all responses to all CORS preflight requests. In practice, this means
2121// all responses to OPTIONS requests.
2222const corsHeaders = {
2323 " Access-Control-Allow-Origin" : " *" ,
24- " Access-Control-Allow-Methods" : " GET, HEAD, POST, OPTIONS" ,
25- " Access-Control-Allow-Headers" : " Content-Type" ,
24+ " Access-Control-Allow-Methods" : " GET,HEAD,POST,OPTIONS" ,
2625 " Access-Control-Max-Age" : " 86400" ,
2726}
2827
@@ -123,16 +122,24 @@ async function handleRequest(request) {
123122function handleOptions (request ) {
124123 // Make sure the necessary headers are present
125124 // for this to be a valid pre-flight request
126- if (
127- request .headers .get (" Origin" ) !== null &&
128- request .headers .get (" Access-Control-Request-Method" ) !== null &&
129- request .headers .get (" Access-Control-Request-Headers" ) !== null
125+ let headers = request .headers ;
126+ if (
127+ headers .get (" Origin" ) !== null &&
128+ headers .get (" Access-Control-Request-Method" ) !== null &&
129+ headers .get (" Access-Control-Request-Headers" ) !== null
130130 ){
131131 // Handle CORS pre-flight request.
132- // If you want to check the requested method + headers
132+ // If you want to check or reject the requested method + headers
133133 // you can do that here.
134+ let respHeaders = {
135+ ... corsHeaders,
136+ // Allow all future content Request headers to go back to browser
137+ // such as Authorization (Bearer) or X-Client-Name-Version
138+ " Access-Control-Allow-Headers" : request .get (" Access-Control-Request-Headers" ),
139+ }
140+
134141 return new Response (null , {
135- headers: corsHeaders ,
142+ headers: respHeaders ,
136143 })
137144 }
138145 else {
0 commit comments