Skip to content

Instantly share code, notes, and snippets.

@ng-hai
Last active January 17, 2021 05:36
Show Gist options
  • Save ng-hai/c9229ef72b601e91b71e85ceb9cc2f49 to your computer and use it in GitHub Desktop.
Save ng-hai/c9229ef72b601e91b71e85ceb9cc2f49 to your computer and use it in GitHub Desktop.
My smoothly config for `nprogress` with `next/router` and use as a component
import Router from "next/router"
import Nprogress from "nprogress"
Nprogress.configure({ showSpinner: false, minimum: 0.01, easing: "linear" })
let timeout
let timer
function startProgress() {
// Only start the progress if the site takes too long to load
timeout = setTimeout(() => {
timer = setInterval(() => {
Nprogress.inc()
}, 100)
}, 166)
}
function stopProgress() {
clearInterval(timer)
clearTimeout(timeout)
Nprogress.done()
}
Router.events.on("routeChangeError", stopProgress)
Router.events.on("routeChangeStart", startProgress)
Router.events.on("routeChangeComplete", stopProgress)
// For using as a component
export default function NProgress() {
return null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment