Shivan M.
—When you are building an application using Next.js 13 and the app directory, you might wonder about the best practices for structuring your application components, routes, and functionality. Although there is no standard for doing this, there are a couple of guidelines that can help you have a more maintainable directory structure for your codebase.
In Next.js 13, nested folder hierarchy defines the route structure for your application. Each folder in the app
directory represents a route that is mapped to a corresponding URL segment.
Although each folder is mapped to a route, the route is not accessible until there is a page.js
or route.js
file in the folder.
This allows us to co-locate other project components in the /app
directory safely.
Outside of route definitions, the main decision to make regarding folder structure pertains to project files (i.e. files that aren’t route definitions).
There are three main organizational strategies for folder structure in Next.js that can serve as guidelines for your project.
app
Below is an example of a folder structure that you can use for your Next.js project where all project files are stored as top-level directories in the app
directory.
In this example, there is a route for a dashboard page, which contains two nested routes called profile
and billing
. Additionally, the dashboard page returns a default page defined by app/dasboard/page.js
. Lastly, the project has a components
directory where we can safely co-locate components, as shown by the button.js
component.
app/ ├─ components/ │ ├─ button.js ├─ dashboard/ │ ├─ billing/ │ │ ├─ page.js │ ├─ page.js │ ├─ profile/ │ │ ├─ page.js ├─ page.js ├─ layout.js
app
DirectoryA different approach would be to store all non-routing code outside of the app
directory:
app/ ├─ dashboard/ components/ ├─ ... lib/ ├─ ...
In this scenario, globally shared code is stored at the root of the app
directory, but context-specific application code is co-located within the directories of the route segments it applies to:
app/ ├─ dashboard/ │ ├─ components/ │ ├─ _lib/ ├─ components/ │ ├─ ... ├─ _lib/ │ ├─ ...
Next.js 13 offers several project organization features that can make maintaining and managing your codebase easier.
In Next.js 13 you can mark a folder as a private
folder by prefixing the folder name with an underscore: _folderName
. This opts the folder and all its sub-folders out of routing. This can be useful when you want to cleanly separate UI logic from routing logic, or when you are trying to consistently organize internal library code in your codebase.
Below is an example of a _lib
directory that remains private even though it exists within the app
directory:
app/ ├─ components/ │ ├─ button.js ├─ dashboard/ │ ├─ billing/ │ │ ├─ page.js │ ├─ page.js │ ├─ profile/ │ │ ├─ page.js ├─ page.js ├─ layout.js ├─ _lib/ │ ├─ format-date.js
You can wrap the name of a folder in parentheses in the app
directory: (folderName)
. This will indicate that the folder should not be included in the route’s URL path. This is useful for organizational purposes. The following example illustrates this pattern, where routes for the dashboard are organized under the dashboard
route group:
app/ ├─ (dashboard)/ │ ├─ components/ │ ├─ _lib/ │ ├─ profile/ │ ├─ billing/ ├─ components/ │ ├─ ... ├─ _lib/ │ ├─ ...
Next.js has extensive documentation about project organization, including the functional elements related to routing.
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.