You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Elysia与TRPC时Response Cookie无法持久化的问题

Cookie无法持久化问题(Remix + Elysia + tRPC 技术栈)

最近使用Bun搭建了基于Remix + Elysia封装tRPC + Prisma的项目,开发了简易登录系统,计划采用JWT Cookie认证流程,但遇到Cookie无法持久化的问题:尽管响应中存在Cookie,且网络工具可识别,但它不会存入本地存储,后续请求也不会携带该Cookie。

相关网络信息

  • Headers(附截图)
  • Cookies(附截图)
  • Stored cookies(附截图)

Elysia 配置代码

const CONFIG = {
  host: '127.0.0.1',
  port: 3000,
  protocol: 'http'
}

new Elysia()
  .use(
    cors({
      credentials: true,
      origin: true,
    }
  ))
  .use(
    trpc(
      router,
      {
        createContext,
      }
    )
  )
  .listen({
    hostname: CONFIG.host,
    port: CONFIG.port,
  })

console.log(`listening on http://${CONFIG.host}:${CONFIG.port}`)

createContext 实现代码

export async function createContext(opts: FetchCreateContextFnOptions) {
  return opts
}

tRPC 登录查询中设置Cookie的代码

ctx.resHeaders.set(
  'Set-Cookie', 
  'hello=world; SameSite=Lax; Path=/; Max-Age=3600; HttpOnly'
)

内容的提问来源于stack exchange,提问作者alexjkni.dev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 15:15:03