如何在NextJS 13中避免动态路由导致的404错误?

huangapple go评论127阅读模式
英文:

How do I prevent a 404 with dynamic routes in NextJS 13?

问题

我有一个使用Next.js 13的应用,它有一个非常简单的文件夹结构:
- app
  - users
    - [id]
      - page.tsx
  - ...
还有一个`page.tsx`文件,内容类似于以下内容:
export default function UserPage({ params }) {
    return <div>{params.id}</div>;
}
然而,当我运行`next dev`并访问`/users/1`时,我得到了一个404错误。我是否遗漏了关于NextJS 13中动态路由如何工作的某些信息?
英文:

I have a Next13 app with an incredibly simple folder structure:

- app
  - users
    - [id]
      - page.tsx
  - ...

And a page.tsx with content similar to the following:

export default function UserPage({ params }) {
    return &lt;div&gt;{params.id}&lt;/div&gt;
}

However, when I run next dev and go to /users/1, I get a 404. Is there something I'm missing about how dynamic routes work in NextJS 13?

答案1

得分: 4

在我的情况下,问题出现在区域设置中。这是版本13.2.x中已知的问题

修复的临时方法是在 next.config.js 中删除区域设置:

/** @type {import('next').NextConfig} */
const nextConfig = {
// i18n: {
//   locales: ["en"],
//   defaultLocale: "en"
// },
  experimental: {
    appDir: true,
  },
}

module.exports = nextConfig
英文:

In my case, the problem was in the locale settings. This is a known bug in version 13.2.x.

A temporary way to fix this is to remove the locale settings in next.config.js:

/** @type {import(&#39;next&#39;).NextConfig} */
const nextConfig = {
// i18n: {
//   locales: [&quot;en&quot;],
//   defaultLocale: &quot;en&quot;
// },
  experimental: {
    appDir: true,
  },
}

module.exports = nextConfig

huangapple
  • 本文由 发表于 2023年3月8日 17:16:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75671229.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定