Back

Dynamic Breadcrumb in Next.js with Parallel Routes

Dynamic Breadcrumb in Next.js with Parallel Routes
TD
Thibault Le Ouay Ducasse

Aug 19, 20244 min read

In this post, we'll dive into the process of creating dynamic breadcrumbs in Next.js using parallel routes. Our goal is to build a breadcrumb component that automatically updates based on the current page and its hierarchy, all while leveraging server-side rendering for optimal performance.

Introduction

Breadcrumbs are an essential navigation aid in web applications, helping users understand their current location within a site's hierarchy. With Next.js 13's introduction of parallel routes, we have new tools to create more dynamic and flexible navigation structures.

You can find the complete, working example for this tutorial on GitHub. The project uses Next.js 15 and demonstrates the implementation of parallel routes alongside our dynamic breadcrumb.

The Challenge

We're building a website that showcases a catalog of dogs and cats. Our primary challenge is to create a breadcrumb that adapts to the user's current location within the site structure.

Project Structure

Our repository is organized as follows:

Desired Breadcrumb Behavior

  1. On the homepage:

Home

  1. On pet pages (e.g., Dogs or Cats):

Home > Dogs or Home > Cats

  1. On individual pet pages:

Here we only have the pet id, so we need to fetch the pet name from the server side and display it in the breadcrumb.

Home > Dogs > Dog Name

The key is to fetch the necessary data on the server side and dynamically update the breadcrumb based on the current route instead of displaying the pet id.

Parallel route 🚀

Parallel routes in Next.js allow us to render multiple pages in the same layout simultaneously. This feature is particularly useful for our breadcrumb implementation as it allows us to maintain a consistent navigation structure across different page types.

Homepage

In our root layout we will use slot to render the breadcrumb component.

We need to create a new folder @breadcrumb in the app folder

Here we create a new file page.tsx that will be responsible for rendering the breadcrumb.

Pet pages

We also want to create a catch all components for the dynamic routes in the app folder, to achieve this we need to create a new file page.tsx in the @breadcrumb/[...all] folder.

This code was taken from the Jeremy's post about dynamic breadcrumbs in Next.js.

Dogs and Cats pages

We need to create a new file page.tsx in the @breadcrumb/dogs/[id] folder, we must follow the exact same structure as our specific pet pages. In this component we will fetch the pet name from the server side and display it in the breadcrumb.

Conclusion

By leveraging Next.js parallel routes and server-side rendering, we've created a dynamic breadcrumb component that updates based on the current route and fetches data efficiently on the server side. This approach provides a smooth user experience while maintaining good performance.

Remember to check out the full working example on GitHub to see how all the pieces fit together in a Next.js 15 project.

Happy coding!