Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nextjs dynamic page routes: What is the purpose of using the spread operator [...example] or [[...example]] in a page variable?

I am currently learning the Nextjs framework. I would say I have a solid grasp of the concepts already, but there are still a few things I am not sure about. One of those things is the spread operator [...var] or [[...var]] in a dynamic page | route file name.

For example this line from next/auth docs:

pages/api/auth/[...nextauth].js

Is there some concrete use case (that can't be achieved using getStaticPaths()), or is it just a naming convention which I don't know about?

like image 855
HynekS Avatar asked Oct 30 '25 19:10

HynekS


1 Answers

pages/api/auth/[...nextauth].js is a catch-all route which means that it will match /api/auth/a but also /api/auth/a/b, /api/auth/a/b/c and so on.

[[...nextauth]].js would be an optional catch all route. So in addition to the regular catch-all route above it would also match /api/auth.

like image 165
Sascha Klatt Avatar answered Nov 02 '25 10:11

Sascha Klatt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!