From aa6a78888032c2429ded9aeb67e29cfd687b4636 Mon Sep 17 00:00:00 2001 From: Daniel Bachler Date: Mon, 27 May 2024 19:14:51 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20add=20cors=20middleware?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- functions/grapher/_middleware.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 functions/grapher/_middleware.ts diff --git a/functions/grapher/_middleware.ts b/functions/grapher/_middleware.ts new file mode 100644 index 00000000000..656c37cd266 --- /dev/null +++ b/functions/grapher/_middleware.ts @@ -0,0 +1,20 @@ +// Respond to OPTIONS method +export const onRequestOptions: PagesFunction = async () => { + return new Response(null, { + status: 204, + headers: { + "Access-Control-Allow-Origin": "*", + "Access-Control-Allow-Headers": "*", + "Access-Control-Allow-Methods": "GET, OPTIONS", + "Access-Control-Max-Age": "86400", + }, + }) +} + +// Set CORS to all /api responses +export const onRequest: PagesFunction = async (context) => { + const response = await context.next() + response.headers.set("Access-Control-Allow-Origin", "*") + response.headers.set("Access-Control-Max-Age", "86400") + return response +}