Last active
January 17, 2021 05:36
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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