Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
ナニモワカラナイ Next.js(App Router)のディレクトリ構成
Search
iromonek39
December 22, 2024
64
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
ナニモワカラナイ Next.js(App Router)のディレクトリ構成
2024/12/19 「フロントエンドチョットデキル ミニ #01」で話した、Next.js(App Router)のディレクトリ構成の話です。
iromonek39
December 22, 2024
Featured
See All Featured
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.4k
HTML-Aware ERB: The Path to Reactive Rendering @ RubyCon 2026, Rimini, Italy
marcoroth
5
830
Principles of Awesome APIs and How to Build Them.
keavy
128
18k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
530
AI: The stuff that nobody shows you
jnunemaker
PRO
10
1.2k
Building AI with AI
inesmontani
PRO
1
1.3k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.4k
Designing Powerful Visuals for Engaging Learning
tmiket
1
610
The Mindset for Success: Future Career Progression
greggifford
PRO
0
540
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
23k
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
3
280
What’s in a name? Adding method to the madness
productmarketing
PRO
24
4.2k
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」をベースにした
まとめ
問題点 ・もっとファイルやフォルダが増えたらどうするの? ・運用に耐えられる? ・他のフレームワークだとどうなる? ・学習コストとか。
ありがとうございました