Next.js 14 Tutorial - 5 - Routing

Codevolution
7 Nov 202307:45

Summary

TLDRこのビデオスクリプトでは、Nixというフレームワークが提供するルーティング機能について詳しく説明されています。Nixでは、ファイルベースのルーティングメカニズムが採用されており、ブラウザでアクセス可能なURLパスは、コードベース内のファイルとフォルダによって定義されます。プロジェクト構造を理解した後、具体的な例を通じて、ルーティングの基本的な規則と、どのようにルートが作成されるかを学びます。まず、ルーティング用のファイルはすべて「app」という名前のフォルダ内に置かれ、ページを表すファイルは「page.js」または「page.tsx」という名前でなければならず、それぞれのフォルダはブラウザのURLのセグメントに対応します。また、デフォルトでルートが「app」フォルダ内にあり、その下のフォルダ名がURLのパスに対応します。例えば、「about」フォルダ内の「page.tsx」は「/about」にマップされ、「profile」フォルダ内の「page.tsx」は「/profile」にマップされます。URLとファイルが一致しない場合、Nixは自動的に404エラーを返します。このビデオスクリプトは、Nixのルーティング機能を理解し、実装するための基本的な情報を提供しています。

Takeaways

  • 🔍 Nix shs(Next.js)は、ファイルシステムベースのルーティングメカニズムを採用しており、ブラウザでアクセス可能なURLパスはコードベース内のファイルとフォルダによって定義されます。
  • 🛠️ プロジェクトは「npx create-next-app@latest」コマンドを使用してセットアップされ、'routing-demo'という名前でプロジェクトが作成されます。
  • 📁 全てのルートはソースフォルダ内の'app'フォルダに配置する必要があり、この構造はフレームワークの規約に従います。
  • 📄 ルートを表すファイルは、TypeScriptを使用している場合は'page.tsx'という名前でなければなりません。
  • 🌐 'app'フォルダ内の'page.tsx'ファイルはドメインのルートにマッピングされ、'localhost:3000'にアクセスすると'Welcome Home'が表示されます。
  • 🔗 各フォルダはブラウザURLの一部としてマッピングされ、'about'や'profile'などの追加ルートも同様に設定できます。
  • 🔄 存在しないURLへのアクセスは自動的に404エラーを返し、これには追加のルータ設定やコードが不要です。
  • ⚙️ Next.jsは「規約より設定」(convention over configuration)のアプローチを採用しており、ファイルとフォルダの構造がルーティングを決定します。
  • 📚 このビデオではルーティングについて詳細を解説しており、将来のビデオでレイアウトについても詳しく説明する予定です。
  • 📺 チャンネル登録をお勧めし、Next.jsに関する更なるコンテンツを提供します。

Q & A

  • 次に克斯はどのようなプロジェクト構造を使用していますか?

    -次にクスはファイルベースのルーティングメカニズムを使用しており、プロジェクト構造は、ルーティングに必要な特定の規約に従う必要があります。

  • 次にクスでルートを作成するために必要なディレクトリは何ですか?

    -次にクスでルートを作成するためには、ソースフォルダ内の 'app' という名前のディレクトリが必要です。

  • ページに対応するファイルはどのように命名する必要がありますか?

    -ページに対応するファイルは、TypeScriptを使用しているかどうかに応じて、'page.js' または 'page.tsx' という名前で命名する必要があります。

  • ブラウザのURLパスは、プロジェクト内のファイルとどのように関連していますか?

    -ブラウザのURLパスは、プロジェクト内の 'app' フォルダ内のファイルとフォルダの構造によって定義されます。各フォルダは、ブラウザのURLのセグメントに対応します。

  • ルートがブラウザで表示されるためには、どのような規約に従う必要がありますか?

    -ルートがブラウザで表示されるためには、ファイルが 'app' フォルダ内にあり、ファイル名が 'page.js' または 'page.tsx' で、またフォルダ名がURLのセグメントに対応している必要があります。

  • 次にクスでルートが見つからない場合、どうなりますか?

    -次にクスでルートが見つからない場合、自動的に404 Not Foundエラーが返されます。開発者が明示的に非一致のルートを処理する必要はありません。

  • 次にクスのルーティングにはどのようなアプローチが採用されていますか?

    -次にクスのルーティングには、設定よりも規約を重視するアプローチが採用されています。ファイルやフォルダの構造がルートを決定するため、外部のルーターのインストールや設定は必要ありません。

  • 次にクスでルートを作成する際に、どのようなコマンドを使用しますか?

    -次にクスでルートを作成する際には、まず 'npx create-next-app' コマンドを使用してプロジェクトを作成し、その後 'npm run dev' コマンドでローカル開発サーバーを実行します。

  • 次にクスでルートがどのように機能するのかを説明してください。

    -次にクスでは、ファイルベースのルーティングを使用して、プロジェクト内のファイルとフォルダ構造に基づいてURLルートが自動的に作成されます。これにより、開発者はルートの設定をコードで記述する必要がなくなります。

  • 次にクスでルートを作成する際に必要なファイルの拡張子は何ですか?

    -次にクスでルートを作成する際に必要なファイルの拡張子は、JavaScriptの場合は '.js' で、TypeScriptの場合は '.tsx' です。

  • 次にクスでルートがどのようにブラウザに表示されるのかを説明してください。

    -次にクスでルートがブラウザに表示されると、'app' フォルダ内のファイル構造に基づいてURLが形成されます。例えば、'app' フォルダ内に 'page.tsx' ファイルがある場合、そのファイルはルートのルートとして表示されます。

  • 次にクスでルートを作成する際に、レイアウトファイルとは何ですか?

    -次にクスでは、ルートが作成されると自動的に 'layout.tsx' ファイルが生成されます。このファイルは、ルートのレイアウトを定義するために使用されますが、詳細は今後のビデオで説明される予定です。

Outlines

00:00

📂 ファイルベースのルーティングの基本

この段落では、Next.jsのルーティング機能について説明しています。Next.jsはファイルベースのルーティングメカニズムを提供しており、プロジェクトの構造に基づいてURLパスが定義されます。まず、新しいNext.jsプロジェクトを作成し、`app`フォルダ内にルーティング用のファイルを配置します。ルーティングには、Next.jsのフレームワークに従って特定の規則に従う必要があります。例えば、すべてのルートは`app`フォルダ内に置かれ、各ファイルは`page.js`または`page.tsx`という名前でなければなりません。また、各フォルダはブラウザのURLのセグメントに対応します。デフォルトでは、`page.tsx`ファイルがルートドメインに対応付けられ、`localhost:3000`にアクセスすると、"Welcome home"というメッセージが表示されます。

05:02

🔗 フォルダ名に基づくルーティングの詳細

この段落では、Next.jsのルーティングについてさらに詳しく説明しています。ルーティングはファイルとフォルダの構造によって決まります。例えば、`about`フォルダ内の`page.tsx`ファイルは`/about`にマップされ、`profile`フォルダ内のファイルは`/profile`に対応します。また、存在しないURLにアクセスした場合、Next.jsは自動的に404 Not Foundエラーを返します。これは、ルーティングのためのコードを書く必要がないことを意味します。ファイルとフォルダの構造がルーティングを決定するNext.jsのアプローチである、規約優先の構成方法を示しています。

Mindmap

Keywords

The video is abnormal, and we are working hard to fix it.
Please replace the link and try again.

Highlights

Nix offers a file system-based routing mechanism.

URL paths in the browser are defined by files and folders within your code base.

All routes must be placed inside the 'app' folder.

Files representing routes should be named 'page.js' or 'page.tsx'.

Folders correspond to URL segments when certain conventions are followed.

The 'app' folder is created by default for routing.

A simple React component can be exported to create a route.

Running 'npm run dev' starts the development server.

The 'page.tsx' file within the 'app' folder maps to the root of your domain.

The 'layout.tsx' file is automatically created and populated by Next.js.

Additional routes can be created by adding folders and 'page.tsx' files within the 'app' folder.

Each folder within 'app' corresponds to a URL segment.

If a URL cannot map to a file, Next.js automatically responds with a 404 not found.

File and folder structure determines the routes, following the 'convention over configuration' approach.

There's no need to install and configure a router with code.

Next.js routing is an innovative method that simplifies the process of creating and managing routes.

The video provides a practical demonstration of setting up and understanding Next.js routing.

The 'app' folder and its conventions are central to the routing system in Next.js.

Next.js allows for a clean and straightforward approach to defining routes in a project.

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)

関連タグ
Nix shesファイルシステムルーティングURLパスReactコンポーネントファイルベース次期プロジェクト编程教育ウェブ開発初心者向け