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
0
47
ナニモワカラナイ Next.js(App Router)のディレクトリ構成
2024/12/19 「フロントエンドチョットデキル ミニ #01」で話した、Next.js(App Router)のディレクトリ構成の話です。
iromonek39
December 22, 2024
Tweet
Share
Featured
See All Featured
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
250
Visualization
eitanlees
150
17k
Darren the Foodie - Storyboard
khoart
PRO
2
2.4k
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
117
110k
WCS-LA-2024
lcolladotor
0
450
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
340
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
120
Avoiding the “Bad Training, Faster” Trap in the Age of AI
tmiket
0
79
Are puppies a ranking factor?
jonoalderson
1
2.7k
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
330
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
920
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
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」をベースにした
まとめ
問題点 ・もっとファイルやフォルダが増えたらどうするの? ・運用に耐えられる? ・他のフレームワークだとどうなる? ・学習コストとか。
ありがとうございました