File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed
content/pages/platform/functions/8_Function examples Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change 1+ ---
2+ type : example
3+ summary : A Pages Functions for appending CORS headers.
4+ tags :
5+ - CORS
6+ pcx-content-type : configuration
7+ title : Adding CORS headers
8+ weight : 1002
9+ layout : example
10+ ---
11+
12+
13+ ``` js
14+ -- -
15+ filename: / functions/ _middleware .js
16+ -- -
17+
18+ // Respond to OPTIONS method
19+ export const onRequestOptions: PagesFunction = async () => {
20+ return new Response (null , {
21+ status: 204 ,
22+ headers: {
23+ ' Access-Control-Allow-Origin' : ' *' ,
24+ ' Access-Control-Allow-Headers' : ' *' ,
25+ ' Access-Control-Allow-Methods' : ' GET, OPTIONS' ,
26+ ' Access-Control-Max-Age' : ' 86400' ,
27+ },
28+ });
29+ };
30+
31+ // Set CORS to all /api responses
32+ export const onRequest: PagesFunction = async ({ next }) => {
33+ const response = await next ();
34+ response .headers .set (' Access-Control-Allow-Origin' , ' *' );
35+ response .headers .set (' Access-Control-Max-Age' , ' 86400' );
36+ return response;
37+ };
38+
39+ ```
You can’t perform that action at this time.
0 commit comments