Skip to content

Instantly share code, notes, and snippets.

@lamualfa
Last active October 12, 2021 07:21
Show Gist options
  • Save lamualfa/fc53f45eaac1bc630e7c0329e4c821d9 to your computer and use it in GitHub Desktop.
Save lamualfa/fc53f45eaac1bc630e7c0329e4c821d9 to your computer and use it in GitHub Desktop.
Add NProgress to Svelte Kit project

Step

  1. Install nprogress:
npm i nprogress
  1. Put the below code in the src/routes/__layout.svelte (or something else).
<script>
	import 'nprogress/nprogress.css'
	import nprogress from 'nprogress'
	import { onMount } from 'svelte'

	onMount(() => {
		const onNavigationStart = () => {
			nprogress.start()
		}

		const onNavigationEnd = () => {
			nprogress.done()
		}

		window.addEventListener('sveltekit:navigation-start', onNavigationStart)
		window.addEventListener('sveltekit:navigation-end', onNavigationEnd)

		return () => {
			window.removeEventListener('sveltekit:navigation-start', onNavigationStart)
			window.removeEventListener('sveltekit:navigation-end', onNavigationEnd)
		}
	})
</script>

<slot />
  1. Done.

When you navigate to another page, you will see a blue bar at the top of the page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment