You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Next.js 15嵌套动态路由[endpoint]/[entry]访问404求助

解决Next.js 15嵌套动态路由404问题

根据你的描述和代码,访问/aaa/bbb出现404的核心原因大概率是路由文件夹结构不符合App Router的嵌套规则,以下是具体排查和修复步骤:

1. 确认正确的文件夹层级

Next.js App Router中,嵌套动态路由[endpoint]/[entry]需要严格遵循层级结构:

app/
└── [endpoint]/
    ├── page.tsx          # 对应 /[endpoint] 路由
    └── [entry]/
        └── page.tsx      # 对应 /[endpoint]/[entry] 路由

如果你的[entry]文件夹是和[endpoint]同级(而非嵌套在[endpoint]内部),那么/aaa/bbb这个路由根本不存在,自然会返回404。

2. 优化参数类型定义(可选,提升代码严谨性)

虽然这不是导致404的直接原因,但Next.js App Router中params已不再是Promise类型,推荐使用标准写法简化代码:
对于[endpoint]/page.tsx:

type Props = {
  params: { endpoint: string }
}

const Page = async ({ params }: Props) => {
  return <div>{params.endpoint}</div>
}

export default Page;

对于[endpoint]/[entry]/page.tsx:

type Props = {
  params: { endpoint: string; entry: string }
}

export default async function Page({ params }: Props) {
  return <h1>{params.endpoint}{params.entry}</h1>
}

3. 重启开发服务器

修改文件夹结构后,务必重启next dev开发服务器,确保路由配置被重新加载。

额外排查点

  • 检查next.config.js中是否有自定义rewrites/redirects规则干扰动态路由匹配
  • 确认页面文件名是page.tsx(注意小写,避免拼写错误)

内容的提问来源于stack exchange,提问作者Ryan Xu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 03:34:50