From d35dc83cf2e50b6d75ed692e43fc56128882695b Mon Sep 17 00:00:00 2001 From: platane Date: Sat, 6 Jul 2024 11:38:34 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20fix=20cors=20issue?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/github-user-contribution/[userName].ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/github-user-contribution-service/api/github-user-contribution/[userName].ts b/packages/github-user-contribution-service/api/github-user-contribution/[userName].ts index 33dd22dea..e719f451e 100644 --- a/packages/github-user-contribution-service/api/github-user-contribution/[userName].ts +++ b/packages/github-user-contribution-service/api/github-user-contribution/[userName].ts @@ -5,7 +5,18 @@ export default async (req: VercelRequest, res: VercelResponse) => { const { userName } = req.query; try { - res.setHeader("Access-Control-Allow-Origin", "https://platane.github.io"); + // handle CORS + { + const allowedOrigins = [ + "https://platane.github.io", + "https://platane.me", + ]; + const reqOrigin = + req.url && new URL(req.url, `http://${req.headers.host}`)?.origin; + const allowedOrigin = allowedOrigins.find((o) => o === reqOrigin); + if (allowedOrigin) + res.setHeader("Access-Control-Allow-Origin", allowedOrigin); + } res.setHeader("Cache-Control", "max-age=21600, s-maxage=21600"); res.statusCode = 200; res.json(