Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
ナニモワカラナイ Next.js(App Router)のディレクトリ構成
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
iromonek39
December 22, 2024
48
0
Share
ナニモワカラナイ Next.js(App Router)のディレクトリ構成
2024/12/19 「フロントエンドチョットデキル ミニ #01」で話した、Next.js(App Router)のディレクトリ構成の話です。
iromonek39
December 22, 2024
Featured
See All Featured
Why You Should Never Use an ORM
jnunemaker
PRO
61
9.8k
How STYLIGHT went responsive
nonsquared
100
6k
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
760
30 Presentation Tips
portentint
PRO
1
270
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9k
Color Theory Basics | Prateek | Gurzu
gurzu
0
290
Reflections from 52 weeks, 52 projects
jeffersonlam
356
21k
Ruling the World: When Life Gets Gamed
codingconduct
0
190
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
320
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
0
270
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
220
Tell your own story through comics
letsgokoyo
1
890
Transcript
ナニモワカラナイ Next.js(App Router)のディレクトリ構成 2024/12/19 フロントエンドチョットデキル ミニ #01
oken(@displaycontents) ちょっと株式会社 フロントエンドエンジニア 自己紹介 もつ煮ときゅうりが好きです。 Next.jsとTypeScriptとCSSも好きです。
Next.js(App Router)のディレクトリ構成どうしてますか? ・featureベース?layerベース? ・Container/Presentationalパターン? ・Atomic Design? ・Clean Architecture? ・feature sliced
design?
いつも迷います。 0→1で作る場合でも、運用する場合でも。
それぞれメリット・デメリットはあるものの、正解がない。
こんな構成にしてみた。という話です。
今回取り入れたもの ・featureベース ・Container/Presentationalパターン ・balletproof-reactを参考にした ・Colocation
featureベース(package by feature)とは?
featureベース(package by feature)とは? という話をする前に・・・
layerベース(package by layer)とは? レイヤーごとにディレクトリを分ける構造。 フロントエンドにはあまり馴染みのないMVC(Model・View・Controller)のような形。
Next.jsでいうとcomponents/とかhooks/とかapi/とかlib/とかで区切られているよう なイメージ。 layerベース(package by layer)とは?
featureベース(package by feature)とは? 機能別にディレクトリを分ける構造。
featureベース(package by feature)とは? Next.js(App Router)の簡単な例でいうと、src/app/内にページごとに区切られてい るようなイメージ。
Container/Presentationalパターンとは? Container層とPresentational層に分けて、関心の分離をする方法。 ・Container層 ロジックの部分。APIのfetchや処理の部分。 ・Presentational層 UIの部分。
というわけで、こんな感じのディレクトリになりました。
None
ディレクトリの内容 app/ 基本的にはpage.tsxのみ。featuresで定義したコンポーネントを使用。 components/ Presentational層。UIのみ。テスト用のStory。 features/ Container層。APIのfetch処理やそれを使用したコンポーネント。 RSCで直接fetch。また、Unitテスト用のファイル。
今回の設計パターンを採用した理由 1. Next.jsのApp Routerを使用していること。 2. テスト戦略を気にした。 3. Next.jsのドキュメントの「Project Structure」をベースにした。
1. Next.jsのApp Routerを使用していること App RouterではPages RouterのようにgetStaticPropsとかgetServersidePropsでfetchを しなくても良い。 RSC(非同期コンポーネント)で直接fetchができる。 Propsでバケツリレーをする必要がなくなる。 ※page.tsxでfetchしてPropsしていくこともできる。
None
2. テスト戦略を気にした UIとロジックを分離することで、それぞれテストの容易性や目的を明確した。 今回の場合だと、UIの部分にStorybook、featuresの部分にunitテストなどを入れてい る。
Unitテスト stories
Next.js自体は特にこだわりがないらしい。 ただ、いくつかパターンを提供しており、その中の一つで「Colocation」をあげている。 tsxやcss、testなどのファイルが1つのフォルダに同居している状態。 3. Next.jsのドキュメントの「Project Structure」をベースにした
まとめ
問題点 ・もっとファイルやフォルダが増えたらどうするの? ・運用に耐えられる? ・他のフレームワークだとどうなる? ・学習コストとか。
ありがとうございました