Next.js 14 Tutorial - 5 - Routing

Codevolution
7 Nov 202307:45

Summary

TLDRThis tutorial introduces the file system-based routing mechanism in Next.js, a popular React framework. It explains the conventions required for creating routes, such as placing them within an 'app' folder and naming them 'page.js' or 'page.tsx'. The video demonstrates how to set up the root route and additional routes using simple React components. It also covers how Next.js automatically handles 404 errors for non-matching routes, showcasing the framework's convention-over-configuration approach to routing.

Takeaways

  • πŸ˜€ Creating a new Next.js project can be done using the command `npx create-next-app`.
  • πŸ“‚ The routing in Next.js is file-system based, meaning URLs are defined by the file and folder structure within the project.
  • πŸ“ All routes must be placed inside an `app` folder, which is located within the `src` directory.
  • πŸ”‘ Each file representing a route should be named `page.js` or `page.tsx`, depending on whether TypeScript is used.
  • 🏠 The `page.tsx` file within the `app` folder automatically maps to the root of the domain, such as `localhost:3000`.
  • πŸ” Each folder within the `app` directory corresponds to a segment in the browser's URL, and files within these folders define the routes for those segments.
  • πŸ›‘ If a URL does not match any file in the `app` folder, Next.js automatically responds with a 404 Not Found error.
  • 🎨 Customizing routes involves creating new folders within the `app` directory and adding `page.tsx` files with React components.
  • πŸ”„ The `_layout.tsx` file is automatically created by Next.js when the root route is loaded, though it was initially deleted in the script.
  • πŸ“ Following Next.js conventions is crucial for routing to work correctly, emphasizing the framework's 'convention over configuration' philosophy.
  • πŸ” Understanding file-based routing simplifies the process of setting up routes, as there is no need for explicit router configuration with code.

Q & A

  • What is the primary feature of Next.js that the script discusses?

    -The script primarily discusses the file system-based routing feature offered by Next.js.

  • What command is used to create a new Next.js project?

    -The command used to create a new Next.js project is 'npx create-next-app'.

  • What is the role of the 'app' folder in Next.js routing?

    -The 'app' folder is where all the routes must be placed in Next.js, and it is a convention to follow for file-based routing.

  • What should be the naming convention for files representing routes in the 'app' folder?

    -Files representing routes should be named 'page.js' or 'page.tsx' depending on whether JavaScript or TypeScript is used.

  • How does Next.js handle the root route of a domain?

    -Next.js maps the 'page.tsx' file within the 'app' folder to the root of the domain.

  • What happens when a user navigates to a URL that does not correspond to a file in the 'app' folder?

    -Next.js automatically responds with a 404 Not Found response if the URL does not map to a file in the 'app' folder.

  • What is the main advantage of Next.js over traditional React applications?

    -The main advantage of Next.js over traditional React applications is the built-in support for server-side rendering, which can improve performance and SEO.

Outlines

00:00

πŸš€ Introduction to Nix's File-Based Routing Mechanism

The video begins by explaining the file-based routing mechanism offered by Nix, focusing on the app router. It describes how URL paths are defined by files and folders within the code base. The video walks through creating a new Next.js project using the command 'npx create-next-app@latest' and setting up the project structure. It emphasizes the importance of the 'app' folder and the naming conventions for route files, specifically 'page.js' or 'page.tsx'. The example demonstrates creating a simple route that displays a 'welcome home' message when navigating to 'localhost:3000'.

05:02

🌐 Creating Additional Routes and Understanding Conventions

The second part of the video covers creating additional routes for an 'About' page and a 'Profile' page. It explains the conventions that each folder in the 'app' directory corresponds to a URL segment, and each route file should be named 'page.tsx'. The process involves creating folders named 'about' and 'profile' within the 'app' folder and adding 'page.tsx' files with respective React components. The video shows how navigating to 'localhost:3000/about' and 'localhost:3000/profile' displays the respective pages. It also touches on the automatic handling of non-matching routes by Next.js, resulting in a 404 response.

Mindmap

Keywords

πŸ’‘Nix

Nix is a package manager for Linux and Unix systems that focuses on reproducible builds and easy rollbacks. In the context of the video, 'Nix' is a likely misspelling or mispronunciation of 'Next.js', which is a popular JavaScript framework for building user interfaces and web applications. The video discusses the routing feature of Next.js, showing how it simplifies the creation and management of web application routes.

πŸ’‘Next.js

Next.js is a JavaScript framework for building user interfaces and web applications. It is known for its server-side rendering capabilities and ease of use. The video script refers to Next.js as 'Nix shes', which seems to be a mispronunciation or typo. The main theme of the video is to demonstrate how to use Next.js's file system-based routing mechanism.

πŸ’‘Routing

Routing is a fundamental concept in web development that refers to the process of determining how URLs are mapped to the code that generates the content for a website. In the video, the routing feature of Next.js is explored, specifically how it uses a file system-based approach where URL paths are defined by files and folders in the project's codebase.

πŸ’‘App Router

The app router in Next.js is a file system-based routing mechanism that allows developers to define routes based on the file and folder structure within the 'app' directory of a Next.js project. The video explains how this router works and how it simplifies the process of creating and managing routes in a Next.js application.

πŸ’‘File System-Based Routing

File system-based routing is a routing strategy used by Next.js where the URL paths are derived from the file and folder names within the 'app' directory. This approach eliminates the need for explicit route configuration and adheres to the 'convention over configuration' philosophy. The video provides an example of how to implement this type of routing in a Next.js project.

πŸ’‘Page Component

In Next.js, a page component is a React component that is automatically associated with a route based on its file name and location within the 'app' directory. The video script mentions creating 'page.tsx' files to define routes, such as '/page.tsx' for the homepage and '/about/page.tsx' for the about page.

πŸ’‘Convention Over Configuration

Convention over configuration is a design philosophy where the software makes reasonable assumptions about the user's intentions based on established conventions, reducing the need for explicit configuration. Next.js follows this philosophy in its routing system, as demonstrated in the video by the automatic creation of routes based on file and folder names.

πŸ’‘404 Not Found Response

A 404 Not Found response is an HTTP status code indicating that the requested resource could not be found on the server. In the context of the video, it is mentioned that if a URL does not correspond to a file in the 'app' folder, Next.js will automatically return a 404 response, which simplifies error handling for non-matching routes.

πŸ’‘VS Code

VS Code, or Visual Studio Code, is a popular source code editor developed by Microsoft. The video script includes instructions on how to use VS Code to create and manage a Next.js project, navigate the project structure, and implement file system-based routing.

πŸ’‘TypeScript

TypeScript is a superset of JavaScript that adds optional static types, classes, and interfaces to the language. In the video, it is mentioned that the page files can be named 'page.js' or 'page.tsx', with the latter being used when TypeScript is employed in the project for added type safety and development tooling support.

πŸ’‘Layout Component

In Next.js, a layout component is a special component that can be used to wrap page components and provide shared layout functionality across multiple pages. Although not the main focus of the video, the script mentions 'layout.tsx' as a file that Next.js automatically creates and populates when the root route is loaded.

Highlights

Introduction to creating a new Next.js project and understanding its structure.

Exploration of the routing feature in Next.js, focusing on the app router and file system-based routing.

Explanation of how URL paths in the browser are defined by files and folders within the codebase.

Demonstration of creating a new Next.js project using the command 'npx create-next-app'.

The necessity to follow certain conventions for routing in Next.js, emphasizing framework adherence.

Conventions include placing all routes within the 'app' folder and naming route files 'page.js' or 'page.tsx'.

Each folder within the 'app' folder corresponds to a URL segment, automatically creating routes.

Creating a root route that displays a 'Homepage' message when a user visits the website's root URL.

How to implement the root route by creating a 'page.tsx' file within the 'app' folder.

Running 'npm run dev' to see the root route in action at 'localhost:3000'.

Introduction of 'layout.tsx' and its automatic creation by Next.js for the root route.

Creating additional routes for 'About' and 'Profile' pages within the 'app' folder.

Defining routes by creating 'page.tsx' files in corresponding folders like 'about' and 'profile'.

How navigating to 'localhost:3000/about' and 'localhost:3000/profile' renders respective pages.

Understanding that routes are associated with files based on their containing folder's name within 'app'.

Automatic 404 Not Found response by Next.js for URLs that do not map to a file in the 'app' folder.

Emphasis on Next.js's approach of convention over configuration for routing, simplifying the process.

Invitation to continue learning about routing in Next.js in the next video of the series.

Transcripts

play00:00

now that we know how to create a new

play00:02

next year's project and what the project

play00:04

structure looks like it is time to dive

play00:06

into the different features that Nix shs

play00:09

offers in this section we will explore

play00:11

the routing feature offered by Nix shes

play00:14

specifically focusing on the app

play00:17

router next implements a file system

play00:20

based routing mechanism where the URL

play00:22

paths accessible in the browser are

play00:25

defined by files and folders within your

play00:27

code base let's dive into the code code

play00:30

and understand with an

play00:32

example I have opened vs code and as you

play00:35

can see I have created a new next year's

play00:37

project using the

play00:39

command npx create hyphen next hyphen

play00:43

app at latest and the project name is

play00:46

routing hyphen

play00:48

demo once the command is executed you

play00:51

should have a project similar to mine

play00:54

open the project and navigate to The

play00:56

Source

play00:57

folder for a fresh start delete the app

play01:00

folder as we will learn how to create

play01:02

routes in next Shar from

play01:06

scratch now I already mention that Nexes

play01:09

has a file based routing

play01:11

mechanism but does that mean every file

play01:14

in our app corresponds to a route well

play01:17

no if you remember from the intro video

play01:20

I mentioned that nexs is a framework

play01:23

where we need to follow certain

play01:25

conventions routing is one such feature

play01:28

where we have to heavily follow the

play01:30

framework

play01:31

conventions what are those conventions

play01:34

well let's understand with a few

play01:35

different

play01:38

scenarios first all routes must be

play01:41

placed inside the app

play01:43

folder second every file that represents

play01:47

a route should be named page.js or page.

play01:51

TSX depending on whether typescript is

play01:54

used or not third each folder

play01:57

corresponds to a part segment in the

play01:59

browser

play02:00

URL when these conventions are followed

play02:03

the file automatically becomes available

play02:05

as a

play02:07

route for scenario one let's add a route

play02:11

that needs to be rendered when a user

play02:13

visits our website our website is of

play02:16

course localhost 3000 so if the user

play02:19

navigates to Local Host Port 3000 we

play02:21

want to display a message

play02:23

homepage let's head back to vs code and

play02:26

implement this

play02:28

scenario the first important convention

play02:31

you should know is that by default all

play02:34

routes must be placed within a folder

play02:36

called app which will exist within the

play02:39

source folder let's create a new

play02:42

folder called app within

play02:46

Source the second important convention

play02:48

you should know is that every file that

play02:51

corresponds to a route in the browser

play02:53

should be named page.

play02:55

TSX page is a name of the file and TSX

play02:59

is the file

play03:01

extension within the app

play03:03

folder let me create page.

play03:08

TSX in this file I'm going to default

play03:11

export a very simple react

play03:14

component export default function home

play03:18

and we return an each one tag that says

play03:24

homepage the text of course can be

play03:26

anything you want to for example well

play03:29

welcome

play03:31

home and believe it or not we have

play03:34

created our very first route in next

play03:37

shares we can now run the

play03:40

command npm run

play03:46

Dev and when you open Local Host Port

play03:49

3000 you should see the text welcome

play03:52

home so this is the first point to keep

play03:55

in mind about routing in next

play03:58

shares the page. TSX file within the app

play04:02

folder maps to the root of your

play04:06

domain scenario one has been

play04:08

successfully

play04:11

implemented now before we proceed let's

play04:14

discuss the layout. TSX file although we

play04:18

deleted this file along with the app

play04:21

folder next automatically creates and

play04:24

populates it when we load the root route

play04:27

we will cover layouts in detail in

play04:29

future videos but it is important to be

play04:32

aware of this file being magically

play04:35

created next let's move on to scenario 2

play04:39

in this scenario we need two additional

play04:42

routes one route to render when the user

play04:46

visits the about page and another to

play04:48

render when the user visits the profile

play04:51

page let's head back to vs code and

play04:53

Define the two new

play04:56

routes in the app folder I'm going to

play04:59

create a another

play05:01

folder about in

play05:04

lowercase within the folder I'm going to

play05:06

define a new page. TSX

play05:12

file in this file I'm going to default

play05:15

export a react

play05:17

component similarly let's create yet

play05:20

another folder in the app folder called

play05:26

profile create a new page. TSX

play05:30

file copy the react component and change

play05:35

about to

play05:37

profile and the

play05:40

text to my

play05:43

profile if you now save both the files

play05:46

and head back to the

play05:48

browser the homepage should still be

play05:51

displayed at the root route however if

play05:54

you navigate to localhost 3000

play05:57

slab you will see the about me page

play06:01

similarly changing the URL to slash

play06:05

profile will render the my profile page

play06:09

scenario 2 has been

play06:12

implemented therefore the second point

play06:15

to remember is that routes are

play06:17

associated with a file based on the

play06:20

containing folders name within the app

play06:23

folder page. TSX within the about folder

play06:27

Maps toabout

play06:29

while page. TSX within the profile

play06:32

folder Maps to/

play06:35

profile here is a visualization of what

play06:38

we have learned so far page. TSX within

play06:41

the app folder is the root route and

play06:44

then each folder corresponds to a URL

play06:48

segment now what happens if you enter a

play06:51

URL that cannot map to a file in the app

play06:54

folder for

play06:57

example SL dashboard board well next shs

play07:02

will automatically respond with a 404

play07:05

not found response you don't have to

play07:08

explicitly handle a non-matching

play07:10

route hopefully the concept of file

play07:13

based routing should now make more sense

play07:15

to you there is no need to install and

play07:18

configure a router with code your file

play07:20

and folder structure determines the

play07:23

routes this represents next year's

play07:26

approach of convention over

play07:27

configuration for routing

play07:30

we still have more to learn about

play07:32

routing so let's continue in the next

play07:33

video thank you for watching and please

play07:36

consider subscribing to the channel for

play07:38

more content on

play07:44

nexts

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
Next.jsRoutingFile SystemApp RouterWeb DevelopmentReact ComponentsConventionsURL PathsFolder StructureDynamic RoutingWeb Apps