File tree 2 files changed +13
-10
lines changed
2 files changed +13
-10
lines changed Original file line number Diff line number Diff line change 1
- function handler ( event ) {
1
+ function handler ( event ) {
2
2
var request = event . request ;
3
- var headers = request . headers ;
4
- var host = request . headers . host . value ;
5
-
6
- // If origin header is missing, set it equal to the host header.
7
- if ( ! headers . origin )
8
- headers . origin = { value :`https://${ host } ` } ;
9
-
10
- return request ;
3
+ var response = event . response ;
4
+
5
+ // If Access-Control-Allow-Origin CORS header is missing, add it.
6
+ // Since JavaScript doesn't allow for hyphens in variable names, we use the dict["key"] notation.
7
+ if ( ! response . headers [ 'access-control-allow-origin' ] && request . headers [ 'origin' ] ) {
8
+ headers [ 'access-control-allow-origin' ] = { value : request . headers [ 'origin' ] . value } ;
9
+ console . log ( "Access-Control-Allow-Origin was missing, adding it now." ) ;
10
+ }
11
+
12
+ return response ;
11
13
}
Original file line number Diff line number Diff line change 15
15
},
16
16
"headers" : {
17
17
"host" : { "value" : " www.example.com" },
18
- "accept" : { "value" : " text/html" , "multivalue" : [ { "value" : " text/html" }, { "value" : " application/xhtml+xml" } ] }
18
+ "accept" : { "value" : " text/html" , "multivalue" : [ { "value" : " text/html" }, { "value" : " application/xhtml+xml" } ] },
19
+ "origin" : { "value" : " https://www.example.com" }
19
20
},
20
21
"cookies" : {
21
22
"id" : { "value" : " CookeIdValue" },
You can’t perform that action at this time.
0 commit comments