BlogsVikas Ukani

A Full stack Software Engineer, Technical blogger.

View all blogs

Enhancing User Experience with Router Progress Bar in Next.js 14

Enhancing User Experience with Router Progress Bar in Next.js 14
Author 04
Vikas Ukani
Published on 2024-01-26

Introduction:

In the ever-evolving landscape of web development, user experience is paramount. One effective way to enhance user experience is by incorporating a progress bar that provides visual feedback during page transitions. With the release of Next.js 14, integrating a router progress bar has become even more straightforward. In this article, we will explore step-by-step how to add a router progress bar to your Next.js 14 project, complete with a full example and images.

Router Progress Bar

Setting Up a Next.js 14 Project:

Assuming you have Node.js installed, let's start by creating a new Next.js 14 project:

npx create-next-app my-next-app
cd my-next-app
npm install

Now, make sure you are using Next.js version 14 or later:

npm install next@latest

Adding Router Progress Bar:

Next.js 14 comes with a built-in component called TopProgressBar that makes it easy to add a router progress bar.

  1. Open the pages/_app.js file in your project.

  2. Import the TopProgressBar component:

// pages/_app.js
import { TopProgressBar } from 'nextjs-progressbar';
  1. Wrap your Component with TopProgressBar:
// pages/_app.js
function MyApp({ Component, pageProps }) {
  return (
    <>
      <TopProgressBar />
      <Component {...pageProps} />
    </>
  );
}

export default MyApp;

Customizing the Progress Bar:

You can customize the appearance of the progress bar by passing options to the TopProgressBar component. For example:

// pages/_app.js
function MyApp({ Component, pageProps }) {
  return (
    <>
      <TopProgressBar color="#29D" startPosition={0.3} stopDelayMs={200} />
      <Component {...pageProps} />
    </>
  );
}

export default MyApp;
  • color: Sets the color of the progress bar.
  • startPosition: Sets the starting position of the progress bar.
  • stopDelayMs: Sets a delay before the progress bar disappears after the page transition completes.

Example Usage:

Let's create a simple example to test the router progress bar.

  1. Create a new page, pages/about.js:
// pages/about.js
import Link from 'next/link';

const AboutPage = () => {
  return (
    <div>
      <h1>About Us</h1>
      <p>This is the about page.</p>
      <Link href="/">
        <a>Go back to Home</a>
      </Link>
    </div>
  );
};

export default AboutPage;

Testing the Progress Bar:

Start your Next.js app and navigate between pages. You should see the router progress bar in action.

npm run dev

Conclusion:

Integrating a router progress bar in Next.js 14 is a simple yet effective way to provide users with visual feedback during page transitions. With the built-in TopProgressBar component, you can effortlessly enhance the user experience in your Next.js applications. Customize the appearance to match your design, and watch as your users enjoy a smoother and more visually appealing navigation experience. Happy coding!

Vikas Ukani
Vikas Ukani

Hi, I'm Vikas, and I enjoy sharing my journey of learning web development. For more information about me, check out my portfolio website and blogs.