Skip to content

fix(not-found): 改 Server Component 修 Server Action 路径下 useTranslations 崩溃#321

Merged
longsizhuo merged 1 commit intomainfrom
fix/not-found-server-component
Apr 26, 2026
Merged

fix(not-found): 改 Server Component 修 Server Action 路径下 useTranslations 崩溃#321
longsizhuo merged 1 commit intomainfrom
fix/not-found-server-component

Conversation

@longsizhuo
Copy link
Copy Markdown
Member

问题

Sentry [SENTRY-BOLE-NOTEBOOK-2] —— prod 环境 `POST /` 请求触发 not-found 渲染时 `useTranslations` 抛 "No intl context",500 上报 Sentry。

根因

  • 爬虫向 `/` POST 触发 Next.js 的 Server Action 路径(Sentry tag `route_type: action` 已确认)
  • Server Action 路径未匹配到 action 后渲染 `not-found.tsx`,但不经过 root layout
  • `` 不在 React 树里,client hook `useTranslations` 直接抛错

修复

  • `app/not-found.tsx` 改为 Server Component,使用 `getTranslations`(来自 `next-intl/server`)。服务端 API 直接读 `i18n/request.ts`,不依赖 client provider
  • 把 `useEffect` + `usePathname` + umami 埋点拆出 `app/not-found-tracker.tsx` 作为 client 子组件挂载

影响

  • 真实用户:0(事件均来自机器人 POST 探测)
  • Sentry 噪声会清零,避免后续真错被淹没

Test plan

  • tsc --noEmit 通过
  • vitest 通过(lint-staged 跑过)
  • preview 部署后访问 `/` 走 GET → 正常 404 页(中英文双语正确显示)
  • preview 部署后 `curl -X POST https:///` → 不再 500 上报 Sentry

…s 崩溃

爬虫向 / POST 时 Next 走 Server Action 路径,未匹配到 action 后渲染
not-found.tsx 不经过 root layout,NextIntlClientProvider 不在 React 树里,
useTranslations 抛 "No intl context" 直接 500 上报 Sentry。

改用 next-intl/server 的 getTranslations,绕开 client provider 依赖。
umami 埋点拆到 not-found-tracker.tsx 作为 client 子组件挂载。

Sentry: SENTRY-BOLE-NOTEBOOK-2
Copilot AI review requested due to automatic review settings April 26, 2026 11:23
@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 26, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
involutionhell-github-io Error Error Apr 26, 2026 11:36am
website-preview Ready Ready Preview, Comment Apr 26, 2026 11:36am

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

该 PR 修复生产环境在 Server Action 路径(例如爬虫对 / 发起 POST)触发 not-found 渲染时,因未经过 root layout 导致 NextIntlClientProvider 缺失,从而 useTranslations 报 “No intl context” 的 500/Sentry 噪声问题。

Changes:

  • app/not-found.tsx 改为 Server Component,用 next-intl/servergetTranslations 获取文案,避免依赖 client provider。
  • 将原先依赖 useEffect/usePathname/window.umami 的 404 埋点逻辑拆分为独立的 client 子组件 app/not-found-tracker.tsx 并在 404 页挂载。

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
app/not-found.tsx 改为服务端获取 i18n 文案并渲染 404,同时挂载 client 追踪子组件以避免 Server Action 场景下的 intl context 崩溃
app/not-found-tracker.tsx 承载 Umami 404 埋点(client-only hooks 与 window 访问),与 Server Component 解耦

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@longsizhuo longsizhuo merged commit 20a078d into main Apr 26, 2026
10 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants