An In-Depth Look at the New Foundations and Concepts in the Next.js App Router
Jul 27, 2023The Next.js app router is advancing towards full stability with exciting new concepts and foundations. From improved support for layouts to the latest React innovations, this innovative application router is making a significant mark.
Layouts and Routing
The router has introduced a significant convention - layouts. The new Next.js app router boasts built-in support for enhancing the ease of use around layouts and even facilitating nested layouts.
In the app directory, a new file, layout.tsx, defines the route layout for the application. This exported default function, known as the route layout, accepts some children and renders them in an HTML tag. The foundational element of the Next.js app router is the ability to compose different React components, which is precisely how layouts function at their core.
Pages and Components
Page files, marked by the special page.tsx filename, are an essential part of the router. Each page is an addressable piece of UI, composed within the layout. These pages are, by default, React server components. This means the code only runs on the server, adding no additional JavaScript to the client-side for every new component you introduce into your application. Client components can be introduced later for interactivity.
Dynamic Routing
In addition to static routes, the app router supports dynamic route segments. It works by designating a new folder for each route and defining a new page within it. The file system-based router, a well-loved feature of Next.js, remains in place and is even enhanced with the app directory.
Navigating and Fetching Data
Using the built-in Next.js Link component, navigation between routes is a breeze. When it comes to fetching data, the Next.js app router utilizes the latest React innovations to simplify the process. By declaring the component as an asynchronous component, it can await a promise inside its body.
Caching and Metadata
By default, all data fetches are static. The app router supports different cache arguments to fetch new data on every request or to revalidate data, known as Incremental Static Regeneration (ISR). Remarkably, these fetches can be combined within the same route.
Shifting gears to metadata, the app router introduces a new metadata API for better built-in SEO support. This powerful API makes it simple to define metadata and maps it to corresponding link tags automatically, eliminating manual updates to the head element.
The metadata API can accommodate almost any way to define metadata in your application. It also supports a generateMetadata API for when metadata needs to be based on dynamic data. This API accepts the same parameters as the default exported page and allows the return of an object that uses the exact same metadata API.
Conclusion
The Next.js app router brings a host of new and improved features to the table. From easy layouts to dynamic routing, inbuilt SEO support to simplified data fetching, it is ushering in a new era of web development. With continued development and stability, it's certain to become a core component in creating powerful and efficient applications.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras sed sapien quam. Sed dapibus est id enim facilisis, at posuere turpis adipiscing. Quisque sit amet dui dui.
Stay connected with news and updates!
Join our mailing list to receive the latest news and updates from our team.
Don't worry, your information will not be shared.
We hate SPAM. We will never sell your information, for any reason.