honoでhono/client
を利用したRPCモードでのクライアント通信を実装していたところ、CORS対応にて調べた内容。
hono: CORS設定
app.use(
"*",
cors({
origin: "http://localhost:3000",
allowHeaders: [
"X-Custom-Header",
"Upgrade-Insecure-Requests",
"Content-Type",
"Authorization",
],
allowMethods: ["POST", "GET", "OPTIONS"],
maxAge: 600,
credentials: true,
}),
);
その場合のclientではinit: {}
にてcredentials設定を行う。
const _client = hc<ApiType>("http://localhost:5001", {
headers: {
"Content-Type": "application/json",
},
init: {
credentials: "include",
},
});