App Router Reference

Pro Members Only

This lesson is available if you have an active subscription.

Alternatively, some member might be able to gift it to you.

If you already have a subscription, you can sign in.

App Router Reference

Subscription Required

You must have an active subscription to access this content.
If you already have a subscription, you can sign in.

The NextJS App Router is an extremely powerful file based router, designed for high performance and maintainable modern web apps.

In this lesson we provide a reference for its core features so you can use it to:

  • quickly get up to speed.
  • and later on recap whenever you want a refresher.

File Based Router

Folders (πŸ—‚οΈ) within the app directory become a part of the routing.

Example

  • app/page.tsx -> services -> /
  • app/πŸ—‚οΈhello/page.tsx -> services -> /hello

API Routes

Creating a route.ts file creates an API route handler. This works in conjunction with the folder based routing.

Example

  • app/route.ts -> services all http requests (GET / POST etc.) for /
  • app/πŸ—‚οΈhello/route.ts -> services all http requests for /hello

Mixing Pages and Routes

You cannot have a page and route in the same folder. Instead consider nesting the route in an api folder.

Example

  • app/πŸ—‚οΈfaq/page.tsx -> /faq
  • app/πŸ—‚οΈfaq/πŸ—‚οΈapi/route.ts -> /faq/api

Route Groups

Folders with () are called route groups and not used as a part of the route matching.

Example

  • app/page.tsx -> /
  • app/πŸ—‚οΈ(home)/page.tsx -> /

Private Folders

Folders that start with _ are excluded from routing. You can create files in them without worrying if its a reserved nextjs routing file. Use them for utilities.

Example

  • app/πŸ—‚οΈ_lib -> private folder
  • app/πŸ—‚οΈ_lib/page.tsx -> in a private folder -> not routable

Dynamic Route Segment

You can use square brackets [] with a name to match a segment. Note that it only matches a single segment.

Example

  • app/πŸ—‚οΈcourse/πŸ—‚οΈ[courseSlug]/page.tsx ->
    • /course/react βœ…
    • /course/nextjs βœ…
    • /course/nextjs/basics ❌

Dynamic Route Catch-all

You can use square brackets with triple dots [...] to match multiple segments.

Example

  • app/πŸ—‚οΈlesson/πŸ—‚οΈ[...slugs]/page.tsx ->
    • /lesson/nextjs βœ…
    • /lesson/nextjs/routing βœ…
    • /lesson/nextjs/routing/advanced βœ…

Layout Components

You can create a layout.tsx file for a reusable layout component. Layouts are reused between navigation. Layouts also apply to nested routes.

Example

The following folder structure:

πŸ—‚οΈ app
∟ layout.tsx
∟ page.tsx
∟ πŸ—‚οΈ course
∟ page.tsx

Will render layout as a part of / and /course.

Loading Components

You can create a loading.tsx to render a loading state for when a page is rendering.

Example

The following folder structure:

πŸ—‚οΈ app
∟ loading.tsx
∟ page.tsx

Will render:

  • <Loading/> while the page is rendering on the server.
  • <Page/> after the page is rendered.

Parallel Routes

You can create parallel routes by prefixing the folder name with @.

Example

The following folder structure:

πŸ—‚οΈ app
∟ layout.tsx
∟ page.tsx
∟ πŸ—‚οΈ @team
∟ page.tsx
∟ πŸ—‚οΈ @individual
∟ page.tsx

Will render the following on a request for /:

<Layout children={<Page />} team={<Team />} individual={<Individual />} />

Intercepting Routes

You can intercept a route for a sub portion of your routing by using intercepting routes. There are multiple kinds of intercepting routes but a simple one is (...)<route_to_intercept> used for root level route interception. A common use case is to render the intercepted UI in a modal.

Example

The following folder structure:

πŸ—‚οΈ app
∟ πŸ—‚οΈ login
∟ page.tsx
∟ πŸ—‚οΈ lesson
∟ page.tsx
∟ πŸ—‚οΈ (...)login
∟ page.tsx

Will behave as follows:

  • On direct navigation to /login
    • it will render /login/page.tsx
  • For a user already on /lesson a navigation request to /login
    • it will render /lesson/(...)login/page.tsx
javascript
typescript
react
playwright

Enjoy free content straight from your inbox πŸ’Œ

No spam, unsubscribe at any time.

Transcript

00:00

We will look at practical examples of the various router files in their own individual lessons. But in order to appreciate the true breadth of what the next year's router offers, this lesson will provide you with the reference bird's eye view at its score. Next year's works on top of file-based routing. What this means is that if we create a file on the file system within the app folder, for example, page do tsx, it'll automatically service requests for the root URL. Note that we are using TypeScript and TSX for this course, but they can also be JavaScript files, for example, JS or JSX, as long as the default export is a react component.

00:36

Next JS will pick it up and render it as a part of a response. Now we can also create folders within the app directory. For example, a folder called Hello that contains a page TSX and next JS will automatically pick it up as a part of its routing. So when a request comes in for slash hello, next JS will render the page do TSX file found within the hello folder and use that to service the request as we've discussed in a previous lesson. Next, JS also supports API routes, and they also work on top of file-based routing. So within the app folder, if we create a file called Router Ts, next J,

01:09

we'll use this file to service any S TT P requests that come in for slash. It can be a get post or any other HTP method. And similar to pages, we can put our routes within folders as well. So if we create a route Ts within a hello folder, this is what next DS will use to service any HTTP requests that come in for slash hello. Now, question that you might have at this point is what happens if we create a page TSX and a router Ts within the same folder? Webpages also work on top of HTTP when a request will come in for slash faq. Next JS will want to render page tsx and

01:44

therefore this particular route file will be considered invalid. The solution to this problem is pretty simple. Don't put pages and routes in the same folder within the FAQ folder. You can have page TSX to service slash faq, and then you can create a separate folder called API for the router ts file, which will be used to handle FAQ slash api. That's the basics of routing, but let's dig into some advanced patterns. We can also create route groups to organize our code a bit better. We know that we can create a file page, do DSX within the app folder to service route slash requests,

02:17

but it's common to refer to this page as the homepage. So we can actually create a folder with round brackets called home folders within round brackets are called route groups and are not used as a part of the route matching. So even though we have moved our page TSX into the home folder, it is still used to match slash. Another feature of the router is private folders. If you create a folder that starts with underscore, that folder is excluded from next year's routing. So even if you create a file called Page tsx, it'll not be used for any routing.

02:50

You can think of private folders as safe folders, so you can put any utility over here without worrying if it's magically going to become routable. Another Feature of the router is dynamic route segments. We can use query brackets for any folder name. For example, hey, we have a folder course slug within the course folder, and this core slug will match any single segment within the URL, for example, slash course slash react slash course slash next, yes, but it'll not match multiple segments. So if we have slash next J slash basics, these are actually two route segments

03:23

and they will not match this particular route. If you want to match multiple segments, we can actually create a catch all dynamic route. Catch all segments are also created with square brackets, but then the matched portion needs to start with triple dots, so a page within this particular folder will match anything that starts with slash lesson. So slash lesson slash next J is fine slash lesson. Next J routing is fine. Next JS routing advanced is also fine. We can actually read the value for what is matched and we will cover that in our dynamic routing lesson. The next J router also supports layout components

03:57

for common UI portions. For example, we can create a layout of TSX next to page tsx, and when a request comes in for scratch, next J will render out the layout and then pass in the page as children. Now, at this point, you might be wondering why do you need layout of tsx? 'cause after all, you could render out the layout as a part of the page tsx. But the key reason over here is that layouts support nesting as well. So if we create another page under the courses folder, when a request will come in for slash courses, next ts will preserve the layout

04:29

that it has already rendered and just rendered the courses page and pass it to the existing layout. Another feature supported by the next year router is loading components. We can actually create a file called loading TSX next to our page, and now when a request comes in for slash next year will start rendering the default export of the loading file. And in parallel also start rendering the page component. While the page component is still rendering, the user will be presented with the loading component and once the page rendering is complete, next, yes, we'll seamlessly swap out the loading

05:01

for the actual page the next year. Router also supports parallel routes. So within the app folder we have our layout component, we have the page component, and then we can create a folder that starts with at, for example, at team that contains a page, an at individual that contains a page. And when a request comes in for slash next, yes, we'll render the page, the at team page and the at individual page in parallel, and all of these results will be passed to the layout component. Of course, the route page is going to be the children,

05:33

but the parallel routes are passed as named slots. So because the folder starts with add team, that component is passed in as the prop team and add individual is passed in as the prop individual. Another advanced supported by the next year router is intercepting routes. So consider a simple folder structure where we have a login folder that contains a page tsx, and then a lesson folder that contains a page. Now, as you would expect, slash login is serviced by login slash page, and slash lesson is serviced by lesson slash page. Now at this point, if the user is on a lesson page

06:06

and we ask them to log in, they will be taken to the login page and this will be a full page reload. This will result in the user losing some of their context, so we can actually do something better. We can create an intercepting route within the lesson folder. Intercepting routes are created with round brackets that contain dots within them, and a triple dot over here means that it is a route level intercepting route. So we are intercepting the route login folder. Now, if the user is on a lesson page and we ask them to log in, the request will be intercepted by our intercepting route and we can do something custom over here.

06:39

For example, within the intercepting page dot tsx, we can use a model for the login component on a cold request to slash login. The full login page is displayed and on a login request from the lesson page, a custom login page can be displayed in a model. As you can see, a lot of thought has gone into building the next J router. Of course, there is a lot more to routing in next JS, and we will discuss that in detail throughout this course with practical code examples. Thank you for joining me and I'll see you in the next one. I.