From 2cba315214de0f0b250064e7b5bfdec269a44d0b Mon Sep 17 00:00:00 2001 From: chenhang Date: Wed, 10 Jun 2026 10:53:43 +0800 Subject: [PATCH] refactor: move ROUTES from @/lib/routes to @/router/routes --- src/app/auth/components/auth-screen.tsx | 2 +- src/app/chat/components/chat-header.tsx | 2 +- src/app/chat/components/chat-menu-bar.tsx | 2 +- src/app/chat/components/quota-dialog.tsx | 2 +- .../chat/deviceid/[deviceId]/DeepLinkPersist.tsx | 2 +- src/app/error.tsx | 2 +- src/app/layout.tsx | 2 +- src/app/not-found.tsx | 2 +- src/app/page.tsx | 2 +- src/app/splash/components/splash-button.tsx | 2 +- src/lib/index.ts | 5 ++++- src/providers/session-provider.tsx | 16 ++++++++++++++++ src/router/index.ts | 14 ++++++++++++++ src/{ => router}/proxy.ts | 2 +- src/{lib => router}/routes.ts | 0 15 files changed, 45 insertions(+), 12 deletions(-) create mode 100644 src/providers/session-provider.tsx create mode 100644 src/router/index.ts rename src/{ => router}/proxy.ts (98%) rename src/{lib => router}/routes.ts (100%) diff --git a/src/app/auth/components/auth-screen.tsx b/src/app/auth/components/auth-screen.tsx index cba65af7..2dd31ad1 100644 --- a/src/app/auth/components/auth-screen.tsx +++ b/src/app/auth/components/auth-screen.tsx @@ -17,7 +17,7 @@ import { useAuthState, useAuthDispatch } from "@/stores/auth/auth-context"; import { useChatDispatch } from "@/stores/chat/chat-context"; import { useUserDispatch } from "@/stores/user/user-context"; import { useAuthGate } from "@/lib/auth/nextauth"; -import { ROUTES } from "@/lib/routes"; +import { ROUTES } from "@/router/routes"; import { MobileShell } from "@/app/_components/core/mobile-shell"; import { AuthPanel } from "@/app/auth/components/auth-panel"; diff --git a/src/app/chat/components/chat-header.tsx b/src/app/chat/components/chat-header.tsx index 3330145a..6fba6eac 100644 --- a/src/app/chat/components/chat-header.tsx +++ b/src/app/chat/components/chat-header.tsx @@ -11,7 +11,7 @@ import { useRouter } from "next/navigation"; import { useState } from "react"; -import { ROUTES } from "@/lib/routes"; +import { ROUTES } from "@/router/routes"; import { ChatMenuBar } from "./chat-menu-bar"; import styles from "./chat-header.module.css"; diff --git a/src/app/chat/components/chat-menu-bar.tsx b/src/app/chat/components/chat-menu-bar.tsx index 620c2640..88cd65bb 100644 --- a/src/app/chat/components/chat-menu-bar.tsx +++ b/src/app/chat/components/chat-menu-bar.tsx @@ -18,7 +18,7 @@ import { useRouter } from "next/navigation"; import { useEffect, useRef } from "react"; -import { ROUTES } from "@/lib/routes"; +import { ROUTES } from "@/router/routes"; import { SpAsyncUtil } from "@/utils/storage"; import { useAuthDispatch } from "@/stores/auth/auth-context"; import { useUserDispatch } from "@/stores/user/user-context"; diff --git a/src/app/chat/components/quota-dialog.tsx b/src/app/chat/components/quota-dialog.tsx index 62912e19..37c344fe 100644 --- a/src/app/chat/components/quota-dialog.tsx +++ b/src/app/chat/components/quota-dialog.tsx @@ -10,7 +10,7 @@ */ import { useRouter } from "next/navigation"; -import { ROUTES } from "@/lib/routes"; +import { ROUTES } from "@/router/routes"; import styles from "./quota-dialog.module.css"; diff --git a/src/app/chat/deviceid/[deviceId]/DeepLinkPersist.tsx b/src/app/chat/deviceid/[deviceId]/DeepLinkPersist.tsx index 2550d829..c753a45d 100644 --- a/src/app/chat/deviceid/[deviceId]/DeepLinkPersist.tsx +++ b/src/app/chat/deviceid/[deviceId]/DeepLinkPersist.tsx @@ -21,7 +21,7 @@ import { useRouter } from "next/navigation"; import { AuthStorage } from "@/data/storage/auth/auth_storage"; import { StorageKeys } from "@/data/storage/storage_keys"; -import { ROUTES } from "@/lib/routes"; +import { ROUTES } from "@/router/routes"; interface DeepLinkPersistProps { deviceId: string; diff --git a/src/app/error.tsx b/src/app/error.tsx index 6411898b..5b2ead9c 100644 --- a/src/app/error.tsx +++ b/src/app/error.tsx @@ -13,7 +13,7 @@ import { useEffect } from "react"; import Link from "next/link"; -import { ROUTES } from "@/lib/routes"; +import { ROUTES } from "@/router/routes"; interface ErrorProps { error: Error & { digest?: string }; diff --git a/src/app/layout.tsx b/src/app/layout.tsx index e3b0ed8c..27a6ea9f 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -1,7 +1,7 @@ import type { Metadata } from "next"; import { Geist, Geist_Mono } from "next/font/google"; import localFont from "next/font/local"; -import { SessionProvider } from "next-auth/react"; +import { SessionProvider } from "@/providers/session-provider"; import "./globals.css"; import { RootProviders } from "@/providers/root-providers"; diff --git a/src/app/not-found.tsx b/src/app/not-found.tsx index e354a4e3..3c1060c2 100644 --- a/src/app/not-found.tsx +++ b/src/app/not-found.tsx @@ -7,7 +7,7 @@ import Link from "next/link"; -import { ROUTES } from "@/lib/routes"; +import { ROUTES } from "@/router/routes"; export default function NotFound() { return ( diff --git a/src/app/page.tsx b/src/app/page.tsx index 8c53f016..8b5815ec 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -11,7 +11,7 @@ import { redirect } from "next/navigation"; -import { ROUTES } from "@/lib/routes"; +import { ROUTES } from "@/router/routes"; export default function Home() { redirect(ROUTES.splash); diff --git a/src/app/splash/components/splash-button.tsx b/src/app/splash/components/splash-button.tsx index 6ad12dab..314bf3cf 100644 --- a/src/app/splash/components/splash-button.tsx +++ b/src/app/splash/components/splash-button.tsx @@ -21,7 +21,7 @@ import { useAuthState, useAuthDispatch } from "@/stores/auth/auth-context"; import { useChatDispatch } from "@/stores/chat/chat-context"; import { useUserDispatch } from "@/stores/user/user-context"; import { facebookLogin, useAuthGate } from "@/lib/auth/nextauth"; -import { ROUTES } from "@/lib/routes"; +import { ROUTES } from "@/router/routes"; import { LoadingIndicator } from "@/app/_components/core/loading-indicator"; import styles from "./splash-button.module.css"; diff --git a/src/lib/index.ts b/src/lib/index.ts index c3c6e37d..269687e4 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -4,7 +4,10 @@ * 注意:故意**不**把 `src/lib/auth/nextauth.ts`(Client-only)放进来, * 避免 Server bundle 误把 React 客户端代码拉进去。 * 如需导入,请直接 `import { ... } from "@/lib/auth/nextauth";`。 + * + * 注:路由相关常量已迁移至 `@/router`(`src/router/routes.ts`)。 + * 路由守卫已迁移至 `@/router/proxy`(`src/router/proxy.ts`)。 */ -export * from "./routes"; +export * from "./breakpoints"; export * from "./auth/nextauth-helpers"; diff --git a/src/providers/session-provider.tsx b/src/providers/session-provider.tsx new file mode 100644 index 00000000..d22e742c --- /dev/null +++ b/src/providers/session-provider.tsx @@ -0,0 +1,16 @@ +"use client"; +/** + * SessionProvider 客户端包装 + * + * 背景:next-auth v4 的 `SessionProvider`(从 `next-auth/react` 导出) + * **没有** `"use client"` 指令。这是 v4 的历史遗留(v5 / Auth.js 已修正)。 + * + * 当 Server Component(如 `src/app/layout.tsx`)直接 import 时,Next.js + * 不会自动把它当 client component 渲染,会继续在服务端 tree 里执行,导致 + * `useContext` / `useState` 报错: + * "Error: React Context is unavailable in Server Components" + * + * 修复:新建一个显式 `"use client"` 的 wrapper,re-export `SessionProvider`。 + * 这样 Server Component 看到的入口是 `"use client"`,会触发 RSC 边界。 + */ +export { SessionProvider } from "next-auth/react"; diff --git a/src/router/index.ts b/src/router/index.ts new file mode 100644 index 00000000..4e5e10f9 --- /dev/null +++ b/src/router/index.ts @@ -0,0 +1,14 @@ +/** + * Router 公共导出 + * + * 集中管理: + * - 路由常量(ROUTES、ROUTE_BUILDERS、AUTH_ONLY_ROUTES、PUBLIC_ROUTES、ALL_STATIC_ROUTES) + * - Next.js 16 Proxy(CDN 端鉴权重定向) + * + * 业务层导入示例: + * ```ts + * import { ROUTES, AUTH_ONLY_ROUTES, type Route } from "@/router"; + * ``` + */ +export * from "./routes"; +export { proxy, config as proxyConfig } from "./proxy"; diff --git a/src/proxy.ts b/src/router/proxy.ts similarity index 98% rename from src/proxy.ts rename to src/router/proxy.ts index 282e0033..c968f268 100644 --- a/src/proxy.ts +++ b/src/router/proxy.ts @@ -27,7 +27,7 @@ import { AUTH_ONLY_ROUTES, ROUTES, type StaticRoute, -} from "@/lib/routes"; +} from "@/router/routes"; const SESSION_COOKIE_NAMES = [ "next-auth.session-token", diff --git a/src/lib/routes.ts b/src/router/routes.ts similarity index 100% rename from src/lib/routes.ts rename to src/router/routes.ts