Skip to content

Commit 02fd564

Browse files
authored
Merge pull request #133 from MeshJS/bug/cors/subdomain-cache
feat(cors): add cache-busting headers and enhance CORS middleware opt…
2 parents d24de4e + d297d60 commit 02fd564

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/lib/cors.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Cors from "cors";
22
import initMiddleware from "./init-middleware";
3+
import type { NextApiResponse } from "next";
34

45
const rawOrigins = process.env.CORS_ORIGINS || "";
56
const allowedOrigins =
@@ -10,6 +11,8 @@ export const cors = initMiddleware(
1011
methods: ["GET", "POST", "OPTIONS"],
1112
allowedHeaders: ["Content-Type", "Authorization"],
1213
credentials: true,
14+
optionsSuccessStatus: 200, // Some legacy browsers choke on 204
15+
preflightContinue: false,
1316
origin: function (
1417
origin: string | undefined,
1518
callback: (err: Error | null, allow?: boolean) => void,
@@ -55,3 +58,11 @@ export const cors = initMiddleware(
5558
},
5659
}),
5760
);
61+
62+
// Helper function to add cache-busting headers for CORS
63+
export function addCorsCacheBustingHeaders(res: NextApiResponse) {
64+
res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
65+
res.setHeader('Pragma', 'no-cache');
66+
res.setHeader('Expires', '0');
67+
res.setHeader('Vary', 'Origin');
68+
}

src/pages/api/v1/walletIds.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ import { NextApiRequest, NextApiResponse } from "next";
22
import { createCaller } from "@/server/api/root";
33
import { db } from "@/server/db";
44
import { verifyJwt } from "@/lib/verifyJwt";
5-
import { cors } from "@/lib/cors";
5+
import { cors, addCorsCacheBustingHeaders } from "@/lib/cors";
66

77
export default async function handler(
88
req: NextApiRequest,
99
res: NextApiResponse,
1010
) {
11+
// Add cache-busting headers for CORS
12+
addCorsCacheBustingHeaders(res);
13+
1114
await cors(req, res);
1215
if (req.method === "OPTIONS") {
1316
return res.status(200).end();

0 commit comments

Comments
 (0)