How Should You Organize your Nextjs Project
Here is project structure I found helpful and easy to use and understand for a nextjs project I was working on.
I`ll be using this structure going forward on all my projects as its simple, and also easy to understand and use.
Example Project and file structure.
Folders only
Folders and example Files
src/app
This is the “main” directory and most of the UI code and a lot of business logic is kept here.
The organization of this directory will vary depending on the type of app being built.
But a general SAAS or webapp can be organized as following:
src/app/(marketing)
Contains the pages for the marketing site such as the landing page and pricing page.
src/app/auth
Contains the pages for authentication such as the login and signup pages.
src/app/(app)
Pages for the core of the application.
src/app/api
Used to define routes for third party libraries that need a public endpoint. If doing general fetching or mutating of data, server actions or server functions should be used instead.
src/components
very basic building block components that are shared across many pages are kept in components/ui
directory.
More complex components that are shared across several pages
Components that are much more complex and only used by one or two pages are referred to as Page Sections
.
Page Sections
are usually made up of several components and UI-components
and represent an entire area of a page rather than an isolated component.
A collection of Page Sections
form a full page.
Page Section
files are co-located in the same directory as their Parent page in the /app
directory. As opposed to components and UI-components
which are located in their own /component
directory outside the /app directory.
This is because since they are usually only used by one page, its simpler just to keep them near their respective page. Having a Page Section
located in a separate components directory would be overly complex, unnecessary and confusing.
The scale of complexity and building blocks is as follows.
UI-components (e.g button) -> Components (e.g Navbar) -> Page Sections (e.g Hero) -> Page (e.g. Landing)
src/lib
This directory will hold API calls, third party library SDKs, configuration files etc.
This directory will basically hold files, business logic and code that are reused in different App pages
src/lib/API
The API library can be used to hold both api requests to our own db and also third party library api requests.
src/lib/Database
This directory will hold the code for working with our database.
src/lib/Database/(db_model)
Each directory inside the /Database
directory will be corresponding database model in the data schema
mutations.ts and queries.ts
Each data model can have queries or mutations, as per nextjs best practices mutations are setup as server actions and queries as regular server functions.
This allows queries to integrate into nextjs internal caching system.
Mutations don't need to be cached, so server actions are used.
src/lib/Services
Third party services. This will hold code for making API requests and working with third party libraries.
src/lib/Services/(service)
This directory will hold all the required code to work with the third party service or library.
For example src/lib/Services/stripe
will hold files to setup the stripe SDK functions
src/lib/Services/init
The SDK for third party libraries can be initialized here and imported into other files as needed.
For example the stripe SDK can be initialized here with the stripe API key and imported into other files.
src/lib/config
Each file in this directory contains constant variables that are required for different parts of the app.
Defining them here allows consolidation of repeated variables and values. And allows for seamless modifications and updates.
src/lib/types
This directory holds reusable types, enums, third party library custom types, and Zod validations.
/types.ts
Misc types that may be used in multiple files are defined here. Its also acceptable to just define the types or interface in the file its used in.
/enums.ts
Enums are defined here.
/validations.ts
Zod validations and react hook form values are defined here.
src/lib/utils
Different utility files and helper functions can be defined here. Also react context and custom react hooks can be initialized here as well.
Any file or code that doesn't fit in other categories can be defined here as well.
/styles
Files for global styles, fonts and the nextjs Themeprovider
are defined here.
Root Directories
Directories such as e2e testing with playwright
and database prisma
schema can be defined at top level because they generally function independent of the source code and there isn't much code sharing between them.