menard_codes
menard_codes

Reputation: 33

Next.js Route: Dynamic vs exact

I'm new to Next.js and I'm curious what will happen if, for example, I have these two routes where one is dynamic, and one is as-is, and both were almost named the same:

/posts/[id]
/posts/latest

I'm thinking if they will clash with one another? And if they do, then how will Next.JS differentiate that the /latest is not a parameter for /[id]?

Upvotes: 2

Views: 1322

Answers (1)

juliomalves
juliomalves

Reputation: 50288

Next.js handles it internally, it's completely fine to use a predefined route /posts/latest with a dynamic route /posts/[id]. In this case /posts/latest will always take precedence over the dynamic route.

From the 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

Upvotes: 3

Related Questions