Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/lib/cors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Cors from "cors";
import initMiddleware from "./init-middleware";
import type { NextApiResponse } from "next";

const rawOrigins = process.env.CORS_ORIGINS || "";
const allowedOrigins =
Expand All @@ -10,6 +11,8 @@ export const cors = initMiddleware(
methods: ["GET", "POST", "OPTIONS"],
allowedHeaders: ["Content-Type", "Authorization"],
credentials: true,
optionsSuccessStatus: 200, // Some legacy browsers choke on 204
preflightContinue: false,
origin: function (
origin: string | undefined,
callback: (err: Error | null, allow?: boolean) => void,
Expand Down Expand Up @@ -55,3 +58,11 @@ export const cors = initMiddleware(
},
}),
);

// Helper function to add cache-busting headers for CORS
export function addCorsCacheBustingHeaders(res: NextApiResponse) {
res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
res.setHeader('Pragma', 'no-cache');
res.setHeader('Expires', '0');
res.setHeader('Vary', 'Origin');
}
5 changes: 4 additions & 1 deletion src/pages/api/v1/walletIds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ import { NextApiRequest, NextApiResponse } from "next";
import { createCaller } from "@/server/api/root";
import { db } from "@/server/db";
import { verifyJwt } from "@/lib/verifyJwt";
import { cors } from "@/lib/cors";
import { cors, addCorsCacheBustingHeaders } from "@/lib/cors";

export default async function handler(
req: NextApiRequest,
res: NextApiResponse,
) {
// Add cache-busting headers for CORS
addCorsCacheBustingHeaders(res);

await cors(req, res);
if (req.method === "OPTIONS") {
return res.status(200).end();
Expand Down