Next.js 15 Tutorial - 9 - Catch all Segments

Next.js 15 Tutorial - 9 - Catch all Segments

Catchall Segments in Routing

Introduction to Catchall Segments

  • The discussion begins with an introduction to catchall segments, using the example of building a documentation site for a project that has multiple features and concepts.
  • The goal is to create unique routes for each concept under its respective feature, leading to potentially 400 different routes if there are 20 features with 20 concepts each.

Dynamic Routing as a Solution

  • Dynamic routing can significantly reduce the number of files needed by utilizing dynamic route folders with concept IDs, cutting down from 400 files to just 20.
  • Further optimization can be achieved by making the feature folder dynamic as well, reducing it to only two folders: one for feature ID and one for concept ID.

Implementing Catchall Segments

  • To set up catchall segments, create a docs folder and then another folder named with square brackets and three dots (e.g., [...slug]) followed by creating a page.tsx file within this slug folder.
  • This setup allows any URL containing "docs" in the path to render the same page regardless of additional segments, demonstrating how catchall segments work effectively.

Accessing URL Segments in Code

  • To access different URL segments in code, destructure params from props; params.slug will be an array of strings representing the URL parts.
  • Use conditional rendering based on the length of slug to display appropriate content depending on whether it's just a feature or includes both feature and concept.

Optional Catchall Segments

  • Next.js also supports optional catchall segments. By wrapping the slug folder name in an extra pair of square brackets, you can make certain URL paths optional without triggering a 404 error.
  • The choice between using a simple page.tsx versus an optional catchall depends on whether your UI remains consistent across URLs or varies based on them.

Visualization and Conclusion

Video description

Next.js Routing: Catch All Segments Explained In this video, we dive into the powerful concept of catch all segments in Next.js. Learn how to efficiently handle complex routing for a documentation site with dynamic route folders, reducing a potential 400 routes to just a few. Discover how to set up the catch all segments feature, allowing you to capture all URL segments with a single file and maintain a uniform layout for your documentation. Explore practical examples and understand when to use simple vs. optional catch all segment routes. Perfect for organizing and enhancing SEO for a documentation site. ๐Ÿ“” GitHub Repo - https://github.com/gopinav/Next.js-15-Tutorials ๐Ÿ“˜ Frontend Interview Course - https://learn.codevolution.dev/ ๐Ÿ’– Support UPI - https://support.codevolution.dev/ ๐Ÿ’– Support Paypal - https://www.paypal.me/Codevolution ๐Ÿ’พ Github - https://github.com/gopinav ๐Ÿ“ฑ Follow Codevolution + Twitter - https://twitter.com/CodevolutionWeb + Facebook - https://www.facebook.com/codevolutionweb ๐Ÿ“ซ Business - codevolution.business@gmail.com 00:00 Introduction to Catch All Segments 00:56 Dynamic Routing Simplified 01:44 Setting Up Catch All Segments 03:02 Handling URL Segments in Code 04:33 Optional Catch All Segments 05:05 When to Use Catch All Segments 05:57 Conclusion and Next Steps 06:07 Support the Channel