使用i18next的Next.js项目构建报错:找不到/_not-found页面
Next.js + i18next构建报错:找不到"/_not-found"页面的解决方法
问题背景
集成i18next的Next.js 14.2.4项目执行yarn build时触发报错:
Error: Could not find page for path "/_not-found"
完整构建日志:
yarn run v1.22.19 $ next build ▲ Next.js 14.2.4 - Environments: .env Creating an optimized production build ... ✓ Compiled successfully ✓ Linting and checking validity of types ✓ Collecting page data Generating static pages (0/4) [= ]Error: Could not find page for path "/_not-found" at v (/usr/src/app/apps/desk/.next/server/chunks/614.js:1:1346) at Q (/usr/src/app/apps/desk/.next/server/chunks/614.js:12:2151) at nj (/usr/src/app/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:46251) ... { digest: '1530829009' } Error occurred prerendering page "/_not-found". Read more: https://nextjs.org/docs/messages/prerender-error Error: Could not find page for path "/_not-found" at v (/usr/src/app/apps/desk/.next/server/chunks/614.js:1:1346) at Q (/usr/src/app/apps/desk/.next/server/chunks/614.js:12:2151) at nj (/usr/src/app/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:46251) ... ✓ Generating static pages (4/4) > Export encountered errors on following paths: /_not-found/page: /_not-found error Command failed with exit code 1. info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
删除app/[locale]文件夹后构建恢复正常。
解决思路
这个报错是因为本地化路由app/[locale]下缺少对应_not-found页面,导致Next.js预渲染时无法找到多语言版本的404页面,与i18next的路由逻辑冲突。可以通过以下方式修复:
1. 添加本地化版本的404页面
在app/[locale]目录下创建_not-found.tsx(或.jsx)文件,实现基础的404逻辑即可,示例代码:
import { notFound } from 'next/navigation'; export default function LocaleNotFound() { notFound(); return null; }
如果需要多语言文案,可直接通过i18next的t函数引入对应翻译文本。
2. 检查i18next路由配置
确认你的i18next配置文件(如next-i18next.config.js)中,路由本地化规则已覆盖特殊页面,确保Next.js能正确映射所有语言的_not-found路由。
3. 静态导出场景补充配置
若项目使用output: 'export'模式,需在next.config.js中确保i18n配置的locales列表完整,且预渲染参数包含所有语言版本,避免遗漏_not-found页面的静态生成。
临时方案:若暂时不需要本地化404页面,可在next.config.js中配置skipTrailingSlashRedirect: true,但不推荐长期使用,建议优先添加对应本地化页面解决根本问题。
内容的提问来源于stack exchange,提问作者AntonAL
相关产品推荐
相关产品推荐

