← All Blogs

Progress Bar in Next.js

29 July, 2021
•
~240 reads
Progress Bar in Next.js blog

canonical_url: https://www.ravsam.in/blog/progress-bar-in-next-js/ date: 2021-07-29 07:41:42 UTC published: true tags: nextjs,webdevelopment,webdesign,react title: Progress Bar in Next.js

Display a Progress Bar on route changes in a Next.js app.

Sometimes when we transition from one route to another, it takes a little time to do so due to different factors. Behind the scenes, it may be rendering a complex page component or doing an API call. In such cases, the app looks like it has frozen for some seconds and then suddenly transitions to the next route. This results in a poor UX. In such cases, it is better to add a progress bar to our application which gives our users a sense that something is loading.

In this tutorial, we learn how to implement a progress bar in a Next.js application.

Contents

    1. Installing NProgress
    1. Basic Usage
  • Results

1. Installing NProgress

The first step we need to do is to install nprogress npm module.

npm i --save nprogress

2. Basic Usage

In pages/_app.js, import the following modules:

import Router from 'next/router'
import NProgress from 'nprogress'

Now, we need to add some Router events to control the behaviour of the progress bar. We need to add the following code:

Router.events.on('routeChangeStart', () => NProgress.start())
Router.events.on('routeChangeComplete', () => NProgress.done())
Router.events.on('routeChangeError', () => NProgress.done())

Depending upon our use case, we can remove the loading spinner that comes by default.

NProgress.configure({ showSpinner: false })

The final code for pages/_app.js will look like this:

import Router from 'next/router'
import NProgress from 'nprogress'

Router.events.on('routeChangeStart', () => NProgress.start())
Router.events.on('routeChangeComplete', () => NProgress.done())
Router.events.on('routeChangeError', () => NProgress.done())

NProgress.configure({ showSpinner: false })

function MyApp({ Component, pageProps }) {
  return <Component {...pageProps} />
}

export default MyApp

Results

We are done with the code. Let’s see how our progress bar will look like in a Next.js application.

If you enjoyed my article, follow me for more stuff.

This article was originally published on RavSam’s blog. We publish our articles on Medium after a week.

🤝 Connect with Me

I love writing for the community while working on my freelance and open source projects. Connect with me through Twitter • LinkedIn • Github • Email.

💌 Get our Newsletter

We write about Nuxt, Vue, Strapi, Flutter, Jamstack, and Automation. Subscribe to our newsletter

🛖 About RavSam

We are helping companies and startups around the globe with Digital Product Development powered by modern Jamstack architecture. Get in Touch with us.

📙 You might also enjoy reading


📮 Join my newsletter

I share tips on how to get started with freelancing, remote jobs, developer-related stuff, startup ecosystem, and lots of insider secrets with my subscribers.

Subscribe →
🧠 More Interesting Reads
Great tools for running a Tech startup
To run a successful tech company, you need to use the right tools to bring success to yourself and...
Top Flutter plugins to take your app to next level
Photo by Serhat Beyazkaya on Unsplash Flutter is one of the fastest rising frameworks for developing...
🎬 Introducing Ravgeek: Dev Concepts in 60 Seconds
After years of writing code, debugging endlessly, and explaining APIs to teammates over coffee, I’ve...