Next.js 路线:动态与精确

Next.js Route: Dynamic vs exact

我是 Next.js 的新手,我很好奇,例如,如果我有这两条路线,一条是动态的,一条是原样的,并且几乎都被命名了,会发生什么相同:

/posts/[id]
/posts/latest

我在想他们会不会互相冲突?如果他们这样做了,那么 Next.JS 将如何区分 /latest 不是 /[id] 的参数?

Next.js 在内部处理它,使用预定义路由 /posts/latest 和动态路由 /posts/[id] 完全没问题。在这种情况下 /posts/latest 将始终优先于动态路由。

来自dynamic routes documentation

Predefined routes take precedence over dynamic routes, and dynamic routes over catch all routes. Take a look at the following examples:

  • pages/post/create.js - Will match /post/create
  • pages/post/[pid].js - Will match /post/1, /post/abc, etc. But not /post/create
  • pages/post/[...slug].js - Will match /post/1/2, /post/a/b/c, etc. But not /post/create, /post/abc