Skip to content

Commit 8d8054d

Browse files
committed
Base Access-Control-Allow-Origin on VERCEL_ENV
1 parent a358033 commit 8d8054d

File tree

1 file changed

+9
-19
lines changed

1 file changed

+9
-19
lines changed

src/app/api/contributors/route.ts

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { NextRequest, NextResponse } from "next/server"
22
import contributorData from "../../../../scripts/sync-landing-schema/data.json"
33

44
export const dynamic = "auto"
5+
export const revalidate = 172800
56

67
interface Contributor {
78
id: string
@@ -17,31 +18,20 @@ function getContributorData(): ContributorData {
1718
return contributorData as ContributorData
1819
}
1920

20-
const PRODUCTION_ORIGIN = `https://${process.env.VERCEL_PROJECT_PRODUCTION_URL}`
21-
const ALLOWED_ORIGIN = process.env.VERCEL_URL
22-
? `https://${process.env.VERCEL_URL}`
23-
: "http://localhost:3000"
24-
25-
function isSameOrigin(origin: string, allowedOrigin: string): boolean {
26-
return (
27-
origin.replace(/\/\/www\./, "") === allowedOrigin.replace(/\/\/www\./, "")
28-
)
29-
}
21+
const ALLOWED_ORIGIN =
22+
process.env.VERCEL_ENV === "production"
23+
? `https://${process.env.VERCEL_PROJECT_PRODUCTION_URL}`
24+
: process.env.VERCEL_URL
25+
? `https://${process.env.VERCEL_URL}`
26+
: "http://localhost:3000"
3027

3128
export async function GET(request: NextRequest) {
32-
let origin = request.headers.get("origin")
33-
if (!origin) {
34-
origin = (request.headers.get("referer") || "").replace(/\/$/, "")
35-
}
36-
3729
const headers = new Headers({
3830
"Content-Type": "application/json",
39-
"Access-Control-Allow-Origin": isSameOrigin(origin, PRODUCTION_ORIGIN)
40-
? PRODUCTION_ORIGIN
41-
: ALLOWED_ORIGIN,
31+
"Access-Control-Allow-Origin": ALLOWED_ORIGIN,
4232
"Access-Control-Allow-Methods": "GET",
4333
"Access-Control-Allow-Headers": "Content-Type",
44-
"Cache-Control": "public, s-maxage=86400, stale-while-revalidate=172800",
34+
"Cache-Control": `public, s-maxage=86400, stale-while-revalidate=${revalidate}`,
4535
})
4636

4737
try {

0 commit comments

Comments
 (0)