Next.js Structure
All information mentioned in Pages & Routes (Common Concepts) applies.
- App Router (Preferred)
Use theapp/directory for routing in new projects. Each route file underapp/is a page, where you should apply the rules above (composition, headings, layout, etc.). - Pages Router (Legacy)
Thepages/directory can still be used in existing projects. Keep the standard structure, and remember:- Business logic for SSR/SSG (getServerSideProps, getStaticProps) must remain in the
pages/directory, as it won’t work elsewhere.
- Business logic for SSR/SSG (getServerSideProps, getStaticProps) must remain in the
Example Structure
Section titled “Example Structure”Directorysrc/
Directoryapp/
Directoryposts/
- page.tsx # Page component that composes feature widgets
Directoryfeatures/
Directoryposts/
Directorycomponents/ # Posts-specific components
- PostList.tsx # Posts list component
Directoryhooks/ # Posts-specific hooks
- …