Next.js 15 Tutorial - 5 - Routing

Next.js 15 Tutorial - 5 - Routing

Routing in Next.js: Understanding File-Based Routing

Introduction to Next.js Routing

  • Next.js utilizes a file system-based routing system, where the organization of files and folders in the code determines accessible URLs.
  • A new project named "routing-demo" is created using npx create-next-app, setting up a foundation for demonstrating routing.

Key Conventions in Next.js Routing

  • There are three main conventions for routing in Next.js:
  • All routes must reside within the app folder.
  • Route files should be named page.js or page.tsx, depending on whether TypeScript is used.
  • Each folder represents a segment of the URL path.

Creating Your First Route

  • To create a homepage at localhost:3000, follow these steps:
  • Create an app folder within the source directory.
  • Inside this folder, create a file named page.tsx.
  • Default export a simple React component that returns an <h1> tag with "Welcome Home".
  • Running npm run dev starts the development server, allowing access to the homepage displaying "Welcome Home".

Layout Handling in Next.js

  • The layout.tsx file is automatically generated by Next.js when accessing the root route, even if it was deleted initially. This will be explored further later.

Adding Additional Routes

  • For scenario two, additional routes for an About page and Profile page need to be created:
  • Create an about folder inside the app directory and add a page.tsx file that exports a component returning "About Me".
  • Similarly, create a profile folder with its own page.tsx, exporting a component that returns "My Profile".
  • After saving these files, navigating to localhost:3000/about displays the About Me page while localhost:3000/profile shows My Profile.

Understanding Folder Structure and Error Handling

  • The second key concept is that routes correspond directly to their respective folder names within the app directory (e.g., about maps to /about).
  • If someone attempts to access an undefined URL (like /dashboard), Next.js automatically serves a 404 Not Found response without requiring additional code.

Conclusion on File-Based Routing Benefits

Turn any video into a summary like this

YouTube links, meetings, lectures. With transcripts, search, and chat.

Video description

Understanding Next.js File based Routing In this video, we dive into the core feature of routing in Next.js, focusing on its file system-based routing mechanism. Learn how to structure your files and folders to create accessible URLs. Follow along as we create a new Next.js project and build routes for a homepage, About page, and Profile page using key conventions. Discover the simplicity of not needing additional configuration for routing and understand how Next.js handles non-existing routes with a 404 response. Perfect for those looking to master routing in Next.js! ๐Ÿ“” 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 Next.js Routing 00:16 Setting Up a New Next.js Project 00:43 Understanding File-Based Routing 01:26 Creating Your First Route 03:02 Implementing Additional Routes 04:56 Handling Non-Existing Routes 05:17 Conclusion and Next Steps