Skip to content

Commit d9ad56d

Browse files
committed
add CORS header example
1 parent 82f548a commit d9ad56d

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
```

0 commit comments

Comments
 (0)