メインコンテンツへスキップ
バージョン: 9.x

クロスオリジンでクッキーを送信する

非公式ベータ版翻訳

このページは PageTurner AI で翻訳されました(ベータ版)。プロジェクト公式の承認はありません。 エラーを見つけましたか? 問題を報告 →

APIがフロントエンドとは異なるオリジンに存在し、そこにクッキーを送信したい場合、サーバーでCORSを有効にし、fetchに{credentials: "include"}オプションを指定してリクエストと共にクッキーを送信する必要があります。

tRPCが使用するfetch関数に渡す引数は以下のように変更できます。

app.ts
ts
import { createTRPCClient } from '@trpc/client';
const client = createTRPCClient<AppRouter>({
url: 'YOUR_SERVER_URL',
fetch(url, options) {
return fetch(url, {
...options,
credentials: 'include',
});
},
});
app.ts
ts
import { createTRPCClient } from '@trpc/client';
const client = createTRPCClient<AppRouter>({
url: 'YOUR_SERVER_URL',
fetch(url, options) {
return fetch(url, {
...options,
credentials: 'include',
});
},
});