How to add Nprogress to sveltekit route change


Install nprogress
npm i nprogress
Import Nprogress in +layout.svelte & also nprogress.css to style the bar
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
Now import beforeNavigate and afterNavigate
import { afterNavigate, beforeNavigate, goto } from '$app/navigation';
Create beforeNavigate and afterNavigate functions.
beforeNavigate(async () => {
NProgress.start();
});
afterNavigate(async () => {
NProgress.done();
});
If we don’t want the loading circle we can configure Nprogress
NProgress.configure({ showSpinner: false });
To change style or color, add code to app.postcss
#nprogress .bar {
background: rgb(221, 34, 40) !important;
height: 2px !important;
}

Improve caching and performance in SvelteKit by importing images from src/lib instead of static. Learn why and how this approach works.