Nextjs parallel routes are a game-changer for developers who want to optimize their application's performance. By default, Nextjs uses a single-threaded approach to handle routes, but with parallel routes, you can run multiple routes concurrently, improving page loading times and overall user experience.
This approach is particularly useful for applications with multiple pages or APIs that need to be fetched simultaneously. By leveraging parallel routes, you can reduce the load on your server and improve responsiveness.
Nextjs provides a built-in feature called `getStaticProps` that allows you to pre-render pages at build time, which can be combined with parallel routes to achieve even better performance.
What is Next.js Parallel Routes
Next.js parallel routes allow for the simultaneous rendering of multiple pages within the same layout, giving developers granular control over loading and error states for different layout sections.
This is particularly beneficial in scenarios where different sections of a page load at varying speeds or encounter unique errors. Independent routing enables each route to be handled independently, improving the user experience and simplifying debugging and maintenance.
Imagine a dashboard application that displays the users, the teams, and notifications. If the data in the users’ section takes longer to load, the dashboard can display a loading spinner or a UI specifically for that section.
This level of detail in handling states is made possible by parallel routes, which can render different sections of a page simultaneously, side by side, or conditionally. A delay function can be created to add different delays to each slot's load times, allowing for a more realistic simulation of how different sections of a page load.
To achieve this, developers can create separate loading UI components for each slot, such as the @users slot and the @team slot. By doing so, they can isolate issues to specific sections and provide immediate feedback to the user.
Why Use Next.js Parallel Routes
Parallel routes in Next.js offer a fresh perspective on building dynamic user interfaces, allowing developers to define multiple routes that can coexist within the same view.
By enabling independent code rendering on the same URL and within the same view, parallel routes provide a significant improvement over traditional routing methods. This means a single URL can be associated with multiple views.
Developers can now independently create and arrange discrete sections of a page while still enjoying the benefits of a unified routing system. This approach is particularly beneficial in scenarios where different teams work on various sections of the page.
Parallel routes allow each route to be handled independently, allowing for granular control over loading and error states for different layout sections. This is particularly beneficial in scenarios where different sections of a page load at varying speeds or encounter unique errors.
A clear benefit of parallel routes is their ability to split a single layout into various slots, making the code more manageable. This is particularly advantageous when different teams work on various sections of the page.
Independent Routing
Independent Routing is a powerful feature in Next.js that allows you to handle each route independently, giving you granular control over loading and error states for different layout sections.
This is particularly beneficial in scenarios where different sections of a page load at varying speeds or encounter unique errors. For example, if the data in the users’ section takes longer to load, the dashboard can display a loading spinner or a UI specifically for that section.
You can achieve this by defining loading UI for each slot, as demonstrated in the illustration below. For instance, if the analytics data takes longer to load, you can display a loading spinner specifically for that section, while other parts of the dashboard remain interactive.
To get started, you'll need to create a delay function to add different delays to each slot's load times. This can be done by creating a file called utils.ts inside a lib folder in your app directory.
Each slot is automatically passed to the layout as a prop, which you can use to structure the dashboard page. This means you don't have to import them, making your code more organized and efficient.
By using parallel routes, you can improve the user experience by providing immediate feedback and simplify debugging and maintenance by isolating issues to specific sections. This is especially useful when dealing with complex dashboards that need to display various views like user analytics, revenue metrics, and notifications.
How to Implement Next.js Parallel Routes
To implement Next.js parallel routes, you'll need to create slots using the @folder naming convention. This allows you to define multiple routes that can coexist within the same view and still have its own set of slot definitions.
Each slot is then passed as a prop to its corresponding layout.tsx file. For example, if you have a dashboard page, you can define parallel routes for the users, teams, and notifications sections by creating folders with the @users, @team, and @notification naming convention.
To render these slots dynamically, you can access them as attributes of the props object and pass them to the layout component. This is done by navigating to the app/layout.tsx file and changing the code to accept the slots as props.
How to Use
To use Next.js parallel routes, create a simple demonstration app with a dashboard that displays content views like the users page, notifications page, and team page using parallel routes.
First, create the dashboard page by deleting and pasting the code in app/page.tsx. Define the slots, which are the users, teams, and notifications using the @folder naming convention. These slots will act as the contents rendered or displayed inside our dashboard.
Inside the app folder, create a folder called @users, and inside it, create a file called page.tsx. Add the code to define the @users slot.
To access the three slots we defined as attributes of the props object and render them dynamically, navigate to the app/layout.tsx file and change the code to pass the slots as props.
Each slot here is passed automatically to the layout as a prop, which allows us to create the dashboard.
To define a slot, use the @folder naming convention, and each slot is then passed as a prop to its corresponding layout.tsx file.
In the context of our dashboard example, we would define two distinct slots within the app folder: @team for the team section, and @analytics for the analytics section.
Return
With parallel routes, you can return a specific loading UI for each slot, even if some slots take longer to load than others. This is particularly beneficial in scenarios where different sections of a page load at varying speeds.
Each parallel route is streamed independently to the layout, allowing for individual loading and error states, completely isolated from other parts of the layout. This means you can display a loading spinner or a UI specifically for a section that's taking longer to load, while other parts of the dashboard remain interactive.
By defining loading.tsx and error.tsx files within each slot, you can achieve this level of detail in handling states, improving the user experience and simplifying debugging and maintenance. This is a significant improvement over traditional routing methods, which were limited to linear rendering.
With parallel routes, you can independently create and arrange discrete sections of a page, while still enjoying the benefits of a unified routing system. This enables a more flexible and efficient approach to building dynamic user interfaces.
Benefits and Use Cases of Next.js Parallel Routes
Parallel routes in Next.js offer a fresh perspective on building dynamic user interfaces. They enable independent code rendering on the same URL and within the same view, utilizing slots.
This approach is more flexible and efficient than traditional routing methods, which were limited to linear rendering. A single URL could only be associated with a single view, making it challenging to manage complexity and ensure optimal performance.
Developers can now independently create and arrange discrete sections of a page while still enjoying the benefits of a unified routing system. This is particularly advantageous when different teams work on various sections of the page.
Parallel routes can split a single layout into various slots, making the code more manageable. This is a clear benefit of using parallel routes.
Independent route handling and sub-navigation are also key benefits of parallel routes. They enable developers to define multiple routes that can coexist within the same view and still have its own set of slot definitions.
Managing complexity and ensuring optimal performance can still be challenging, but parallel routes offer a more flexible and efficient approach to building dynamic user interfaces.
Sources
- https://nextjs.org/docs/app/building-your-application/routing/parallel-routes
- https://blog.openreplay.com/parallel-routes-in-nextjs/
- https://www.builder.io/blog/nextjs-14-parallel-routes
- https://www.qed42.com/insights/exploring-routing-flexibility-with-next-js-parallel-routes
- https://www.libenhailu.dev/posts/parallel-routes-in-next-14
Featured Images: pexels.com