Next.js 15 Tutorial - 6 - Nested Routes

Next.js 15 Tutorial - 6 - Nested Routes

How to Implement Nested Routes in Next.js

Overview of File-Based Routing

  • The video begins by discussing Next.js's file-based routing system, explaining how page.tsx files in the app folder map to root domains and subfolder routes.
  • It introduces a scenario where a blog page needs to be set up at localhost:3000/blog, along with two additional nested routes: /blog/first and /blog/second.

Creating the Blog Page

  • To create the blog page, a new folder named blog is added inside the app directory, containing a page.tsx file that exports a simple React component.
  • The component returns an <h1> tag with the text "My Blog", which renders correctly when navigating to localhost:3000/blog.

Setting Up Nested Routes

  • For handling nested routes, two new folders named first and second are created within the blog folder.
  • Each of these folders contains its own page.tsx file. The first one exports a function called FirstBlog, returning an <h1> tag with "First Blog Post". The second exports a function called SecondBlog, returning "Second Blog Post".

Rendering Nested Pages

  • After saving these files, navigating to /blog/first displays the first blog post as expected.
  • Similarly, accessing /blog/second shows the second blog post. This demonstrates how easily nested routes can be implemented in Next.js.

Visualization of Route Structure

  • A visualization is provided showing how each level of routing corresponds to its respective folder structure:
  • Root route maps to the main app folder's page.tsx.
  • The blog route corresponds to the blog folder's page.tsx.
Video description

Mastering Nested Routes in Next.js: A Step-by-Step Guide In this video, we dive deeper into Next.js's file-based routing system, focusing on setting up nested routes. We start by creating a simple blog page that appears at 'localhost:3000/blog' and then set up additional routes for 'localhost:3000/blog/first' and 'localhost:3000/blog/second'. By following along, you'll see how Next.js easily mirrors folder structures in URLs. ๐Ÿ“” 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 Setting Up Nested Routes 00:38 Creating Blog Pages 01:20 Handling Nested Blog Routes 02:20 Visualizing Nested Routes 02:46 Introduction to Dynamic Routes 02:50 Support the Channel