Introduction
In this essay I will explain all default files and folders, that come when creating a Next.js project. Its important to know what each file does even though most of them can be ignored at the beginning. Besides that I will talk about the Structure strategy I learned in my online course and link some more of the Next.js Documentation.
| Overview |
|---|
| Top Level Files are mostly config files |
| Structure strategy that helps me keep the overview of my project |
| Example Structure of the Road to Next |
| Conclusion |
Top Level Files
Most of the top level files are configuration files or auto generated files. While its not necessarily to know all details about each of them, itβs still good to know what files serves wich purpose. In the following diagram there is a overview about all default files that come out of the box when creating a new Next.js project. I will talk about some of the relevant files in the next chapters but wonβt mention the files wich are marked with (a) since they are the ones who get auto-generated.
.
βββ .next/ (a) # Build output & cache generated by Next.js (auto-generated, do not edit)
βββ node_modules/ (a) # Installed npm dependencies (auto-generated)
βββ public/ # Static assets (images, fonts, etc.) served directly
βββ src/
β βββ app/ # App Router root (Next.js routing system)
βββ .gitignore # Files/folders Git should not track
βββ eslint.config.mjs # ESLint configuration (code quality & linting rules)
βββ next-env.d.ts (a) # Next.js TypeScript type definitions (auto-generated)
βββ next.config.ts # Next.js configuration file
βββ package-lock.json (a) # Locked versions of dependencies (auto-generated by npm)
βββ package.json # Project metadata, dependencies, and scripts
βββ postcss.config.mjs # PostCSS configuration (used for CSS processing)
βββ README.md # Project documentation
βββ tsconfig.json # TypeScript configuration
public
One use case of the public folder is for adding all kinds of custom icons (svg) or favicon. Another is to add a robot.txt file (a file thats used to tell the search engine how to crawl your website). But you can even add html file or pdf files (according to chatgpt - should probably double check that). The public folder is great because it works just like your root folder. All assets inside it can be directly accessed with /.
For example, this file
public/avatars/me.pngcan be accessed with
/avatars/me.pngBut there are also some dangers hidden. One being the issue with caching. Next.js wonβt be able to cache anything inside the public folder, since these assets might change. And the second being the potential problem of having two identical paths. Like in this example, where both files are accessible with the same url, wich of course will break the application.
public/
hello
src/app/hello
page.tsx
both are reachable with
/hello| Source |
|---|
| π Next.js Documentation > public-folder |
src
While src is technically an optional folder, I like to add it because it stores most of my code within one common place. I will explore the techniques of structuring the project inside the src folder more in a later chapter.
Good to know
- src/appΒ orΒ src/pagesΒ will be ignored ifΒ appΒ orΒ pagesΒ are present in the root directory.
- If youβre usingΒ src, youβll probably also move other application folders such asΒ /componentsΒ orΒ /lib.
| Source |
|---|
| π Next.js Documentation > src-folder |
eslint.config.mjs
This config file is used for configuring eslint and defining the preferred linting rules. I happend to find a great template of a community member of the Road to Next.
next.config.ts
This config file is the place where I can define Next.js itself. Here I can tell how it should behave for example when building the application. So far I happend to use this config file for experimenting with the react complier or for telling Next to use typed routes. futureLinkποΈ β look config in R2N App
package.json
This config file is where all my custom scripts are defined. And its also the place where all dependencies for the dev and prod environment are defined.
postcss.config.mjs
This config file is mainly used for supporting the Tailwind CSS features if enabled during the project setup. But besides that I havenβt had any contact with this file but im sure it can do way more - should explore it another time more deeply.
tsconfig.json
This files purpose is to tell typescript how to type-check my code an how strict it should be. So far I haven touched it and just kept the default.
Structure strategy
There is no correct way on how to structure the projects. With that said, I will just go with the one learned in the Road to Next. But its still worth to have a look at the examples of next.js.
src/app
The app folder will contain all pages that should get their own route. The only thing thats required is a page.tsx file within the folder. Be aware that only pages defined within the app folder can become a route, all other locations are invisible for the App Router.
So for example this page
src/app/users/page.tsxwill have this route
/users
and this url
http://localhost:3000/userssrc/component
The component folder is the perfect place for adding components that can be shared across different projects. Itβs also the place where shadcn will save its components:
src/components/uiSo fare I happend to use the components folder for
- the theme-provider and theme-switcher
- a form with action state futureLinkποΈ
src/features
The features folder is the place where I can store all components that are explicitly created for this project and wonβt work in other projects. This is often the case for database queries, actions or project specific components.
src/path.ts
This file is the perfect place for storing all app routes in one place. This enables a better overview and easier handling of routes. futureLinkποΈ
src/data.ts
The place I can store my test data.
Example Structure of the Road to Next
src/
βββ app/ # App Router with all routes
β βββ tickets/
β β βββ page.tsx
β β
β βββ globals.css
β βββ layout.tsx
β βββ page.tsx
β
βββ components/ # Shared components across projects
β βββ form/
β β βββ hooks/
β β β βββ use-action-feedback.ts
β β β
β β βββ utils/
β β β βββ to-action-state.ts
β β β
β β βββ field-error.tsx
β β βββ form.tsx
β β βββ submit-button.tsx
β β
β βββ theme/
β β βββ theme-provider.tsx
β β βββ theme-switcher.tsx
β β
β βββ ui/ # Shadcn only
β βββ card-compact.tsx
β βββ header.tsx
β βββ heading.tsx
β βββ placeholder.tsx
β βββ spinner.tsx
β
βββ features/ # Project specific features
β βββ ticket/
β βββ actions/
β β βββ delete-ticket.ts
β β βββ upsert-ticket.ts
β β
β βββ components/
β β βββ ticket-item.tsx
β β βββ ticket-list.tsx
β β βββ ticket-upsert-form.tsx
β β
β βββ queries/
β β βββ get-ticket.ts
β β βββ get-tickets.ts
β β
β βββ constants.tsx
β
βββ paths.ts # All router paths
βββ data.ts # Dummy dataConclusion
Creating the perfect Project Structure might be impossible. But the good thing is, its not even necessarily. I think what matters is to just choose one that fits my needs and stick with it for a while. Sooner or later I will see its strength and weaknesses and can adopt it further.