File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ "use client" ;
2+
3+ import { useEffect } from "react" ;
4+ import { usePathname } from "next/navigation" ;
5+
6+ // 从 not-found.tsx 拆出来的 umami 404 埋点。
7+ // 拆分原因:not-found.tsx 必须保持 Server Component(见同目录 not-found.tsx 注释),
8+ // useEffect / usePathname / window.umami 只能在 client。
9+ export default function NotFoundTracker ( ) {
10+ const pathname = usePathname ( ) ;
11+
12+ useEffect ( ( ) => {
13+ if ( window . umami ) {
14+ window . umami . track ( "error_404" , {
15+ path : pathname ,
16+ referrer : document . referrer || "direct" ,
17+ } ) ;
18+ }
19+ } , [ pathname ] ) ;
20+
21+ return null ;
22+ }
Original file line number Diff line number Diff line change 1- "use client" ;
2-
31import Link from "next/link" ;
4- import { useEffect } from "react" ;
5- import { usePathname } from "next/navigation" ;
6- import { useTranslations } from "next-intl" ;
2+ import { getTranslations } from "next-intl/server" ;
73import { Button } from "@/app/components/ui/button" ;
4+ import NotFoundTracker from "./not-found-tracker" ;
85
9- export default function NotFound ( ) {
10- const pathname = usePathname ( ) ;
11- const t = useTranslations ( "notFound" ) ;
12-
13- useEffect ( ( ) => {
14- if ( window . umami ) {
15- window . umami . track ( "error_404" , {
16- path : pathname ,
17- referrer : document . referrer || "direct" ,
18- } ) ;
19- }
20- } , [ pathname ] ) ;
6+ // 必须是 Server Component:爬虫向 / 发 POST 时 Next 走 Server Action 路径,
7+ // not-found 渲染不经过 layout,NextIntlClientProvider 不在树里,
8+ // useTranslations 会抛 "No intl context"。getTranslations 走 server,
9+ // 直接读 i18n/request.ts,没有 provider 依赖。
10+ export default async function NotFound ( ) {
11+ const t = await getTranslations ( "notFound" ) ;
2112
2213 return (
2314 < div className = "flex h-screen w-full flex-col items-center justify-center bg-background text-foreground" >
@@ -32,6 +23,7 @@ export default function NotFound() {
3223 < Link href = "/" > { t ( "cta" ) } </ Link >
3324 </ Button >
3425 </ div >
26+ < NotFoundTracker />
3527 </ div >
3628 ) ;
3729}
You can’t perform that action at this time.
0 commit comments