feat(portal-shell): add MSW mock layer with production bundle exclusion (P1-5)

MSW v2.7.0 fallback layer covering dashboard/users/exams/grades domains.
NEXT_PUBLIC_MSW=1 enables browser Service Worker + SSR route handler mock
responses without backend. Production build excludes all mock data via
Turbopack resolveAlias redirecting @/mocks to empty stub.

Acceptance: build bundle (client+server) verified clean of mock strings;
typecheck/lint/vitest (231 tests) all pass.
This commit is contained in:
SpecialX
2026-07-22 14:48:30 +08:00
parent da05c9107a
commit 9358372657
16 changed files with 1292 additions and 67 deletions

View File

@@ -39,6 +39,9 @@ const CLIENT_PROXY_URL = "/api/graphql";
// 生产环境默认启用(未设置或设置为 true 均启用)
const APQ_ENABLED = process.env.NEXT_PUBLIC_APOLLO_APQ !== "false";
// MSW 启用时跳过 SSR 查询P1-5浏览器端由 MSW SW 拦截 /api/graphql
const MSW_ENABLED = process.env.NEXT_PUBLIC_MSW === "1";
/**
* 创建 Apollo Client 实例。
*
@@ -54,7 +57,8 @@ export function createApolloClient(
const isServer = options.serverSide ?? typeof window === "undefined";
const httpLink = new HttpLink({
uri: isServer ? SERVER_APOLLO_ROUTER_URL : CLIENT_PROXY_URL,
// MSW 启用时P1-5SSR 端也走同域 /api/graphql由 Route Handler 返回 mock 数据
uri: isServer && !MSW_ENABLED ? SERVER_APOLLO_ROUTER_URL : CLIENT_PROXY_URL,
// 客户端同域请求cookie 自动随行;服务端:直连 router 不需要 cookie
credentials: isServer ? "omit" : "include",
});