fix(sync): 修复自托管下无法读取 Upstash 配置

app/api/user/{sync,config} 用 @upstash/redis/cloudflare 的
Redis.fromEnv() 创建客户端。该实现只读传入的 env 参数或 Cloudflare
全局绑定,从不回退到 process.env,因此在 Docker / Node 自托管下
url 与 token 恒为 undefined,两个接口必定返回 500,跨设备同步
(收藏、历史、设置)完全不可用。认证走的是 auth.ts 里的
new Redis({ url, token }),所以登录正常,只有用户数据读写失败。

把 auth.ts 中已有的双源回退抽成 lib/server/runtime-env.ts,Redis
客户端抽成 lib/server/redis.ts,两个路由改为惰性获取共享客户端。
惰性创建同时修正了另一个隐患:Cloudflare 的 per-request 绑定在
模块求值阶段不可达,而原先客户端是在模块作用域创建的。

未配置 Upstash 时返回 503 并给出明确说明,而不是当成请求失败。

Fixes #226
This commit is contained in:
can4hou6joeng4
2026-07-31 10:12:40 +08:00
parent 0675c28413
commit 81c46d754e
5 changed files with 95 additions and 42 deletions
+18 -3
View File
@@ -5,20 +5,25 @@
* so they persist across browsers, devices, and PWA installs.
*/
import { Redis } from '@upstash/redis/cloudflare';
import { NextRequest, NextResponse } from 'next/server';
import { authenticationRequiredResponse } from '@/lib/server/api-responses';
import { getServerSession } from '@/lib/server/auth';
import { getRedisClient } from '@/lib/server/redis';
export const runtime = 'edge';
const redis = Redis.fromEnv();
function redisKey(profileId: string): string {
const safe = profileId.replace(/[^a-zA-Z0-9_-]/g, '');
return `user:config:${safe}`;
}
function syncUnavailableResponse() {
return NextResponse.json(
{ error: 'Server-side sync is not configured on this deployment' },
{ status: 503 }
);
}
export async function GET(request: NextRequest) {
const session = await getServerSession(request);
const profileId = session?.profileId;
@@ -27,6 +32,11 @@ export async function GET(request: NextRequest) {
return authenticationRequiredResponse();
}
const redis = getRedisClient();
if (!redis) {
return syncUnavailableResponse();
}
try {
const data = await redis.get(redisKey(profileId));
return NextResponse.json({ success: true, data: data || null });
@@ -47,6 +57,11 @@ export async function POST(request: NextRequest) {
return authenticationRequiredResponse();
}
const redis = getRedisClient();
if (!redis) {
return syncUnavailableResponse();
}
try {
const body = await request.json();
const key = redisKey(profileId);
+19 -4
View File
@@ -1,21 +1,31 @@
import { Redis } from '@upstash/redis/cloudflare';
import { NextRequest, NextResponse } from 'next/server';
import { authenticationRequiredResponse } from '@/lib/server/api-responses';
import { getServerSession } from '@/lib/server/auth';
import { getRedisClient } from '@/lib/server/redis';
// 确保这行代码在整个文件中只出现一次
export const runtime = 'edge';
const redis = Redis.fromEnv();
function syncUnavailableResponse() {
return NextResponse.json(
{ error: 'Server-side sync is not configured on this deployment' },
{ status: 503 }
);
}
export async function GET(request: NextRequest) {
const session = await getServerSession(request);
const profileId = session?.profileId;
if (!profileId) {
return authenticationRequiredResponse();
}
const redis = getRedisClient();
if (!redis) {
return syncUnavailableResponse();
}
try {
const data = await redis.get(`user:sync:${profileId}`);
return NextResponse.json({
@@ -31,11 +41,16 @@ export async function GET(request: NextRequest) {
export async function POST(request: NextRequest) {
const session = await getServerSession(request);
const profileId = session?.profileId;
if (!profileId) {
return authenticationRequiredResponse();
}
const redis = getRedisClient();
if (!redis) {
return syncUnavailableResponse();
}
try {
const body = await request.json();
const { history, favorites } = body;