/app
: Contains all the routes, components, and logic for your application, this is where you'll be mostly working from./app/lib
: Contains functions used in your application, such as reusable utility functions and data fetching functions./app/ui
: Contains all the UI components for your application, such as cards, tables, and forms. To save time, we've pre-styled these components for you./public
: Contains all the static assets for your application, such as images./scripts
: Contains a seeding script that you'll use to populate your database in a later chapter.- Config Files: You'll also notice config files such as
next.config.js
at the root of your application. Most of these files are created and pre-configured when you start a new project usingcreate-next-app
. You will not need to modify them in this course.
To improve the navigation experience, Next.js automatically code splits your application by route segments. This is different from a traditional React SPA, where the browser loads all your application code on initial load.
Splitting code by routes means that pages become isolated. If a certain page throws an error, the rest of the application will still work.
Furthermore, in production, whenever <Link>
components appear in the browser's viewport, Next.js automatically prefetches the code for the linked route in the background. By the time the user clicks the link, the code for the destination page will already be loaded in the background, and this is what makes the page transition near-instant!
Learn more about how navigation works.
APIs are an intermediary layer between your application code and database. There are a few cases where you might use an API:
- If you're using 3rd party services that provide an API.
- If you're fetching data from the client, you want to have an API layer that runs on the server to avoid exposing your database secrets to the client.
In Next.js, you can create API endpoints using Route Handlers.
- There are two things you need to be aware of:
-
The data requests are unintentionally blocking each other, creating a request waterfall.
A "waterfall" refers to a sequence of network requests that depend on the completion of previous requests. In the case of data fetching, each request can only begin once the previous request has returned data. This pattern is not necessarily bad.
There may be cases where you want waterfalls because you want a condition to be satisfied before you make the next request. For example, you might want to fetch a user's ID and profile information first. Once you have the ID, you might then proceed to fetch their list of friends. In this case, each request is contingent on the data returned from the previous request.
However, this behavior can also be unintentional and impact performance.
A common way to avoid waterfalls is to initiate all data requests at the same time - in parallel.
In JavaScript, you can use the
Promise.all()
orPromise.allSettled()
functions to initiate all promises at the same time. For example, in data.ts, we're usingPromise.all()
in thefetchCardData()
function.By using this pattern, you can:
Start executing all data fetches at the same time, which can lead to performance gains. Use a native JavaScript pattern that can be applied to any library or framework.
However, there is one disadvantage of relying only on this JavaScript pattern: what happens if one data request is slower than all the others?
-
By default, Next.js prerenders routes to improve performance, this is called Static Rendering. So if your data changes, it won't be reflected in your dashboard.