I want to add per page custom skeleton loaders on page navigation. To do that I need to know which page is currently being loaded.
I have tried this approach with useRouter:
//_app.tsx
useEffect(() => {
const handleStart = () => setLoading(true);
const handleComplete = () => setLoading(false);
router.events.on('routeChangeStart', handleStart);
router.events.on('routeChangeComplete', handleComplete);
router.events.on('routeChangeError', handleComplete);
console.log(router);
}, [router]);
The routechange events are being triggered when starting to navigate, but the route console log does not print the page I'm going to until it's already loaded. I have noticed that the url in the browser is not changing before the page loading is completed.
I need a way to get the current loading page (the pathname or anything that can tell me what is currently loading). Is this even possible?
I am using Next.js, TypeScript and SSR.