Logo

dev-resources.site

for different kinds of informations.

Show a loading screen when changing pages in Next.js App router

Published at
1/14/2025
Categories
nextjs
next13
approuter
react
Author
ajith-k
Categories
4 categories in total
nextjs
open
next13
open
approuter
open
react
open
Author
7 person written this
ajith-k
open
Show a loading screen when changing pages in Next.js App router

Switching from the page router to the app router was quite a challenge for me. It wasn't just due to Next.js itself— in fact, the transition from the old page router code to the new one was fairly smooth. The real difficulty came from the other tools we were using. Apollo Client still isn't fully supported , and @module-federation/nextjs-m is now in maintenance, and more. Despite all these challenges, I actually ended up liking app router. One issue I'm still having is with the changes they made new useRouter(the one from next/navigation).
I ran two identical React apps, one using TanStack Routing and the other with Next.js App Router. Immediately, I noticed a significant delay in page transitions in Next.js. The lag in development mode is actually addressed by the Next.js team here. However, I found that the page transition delay was still noticeably slower in production, compared to the TanStack Router example.

As a result, my next step was to implement a page transition loader in the app. Most implementations I came across used router.events to set up a progress bar on route changes. Unfortunately, the newer versions of Next.js no longer support this approach. In the following section, I'll show you how to set up a progress bar for route changes in Next.js App Router using react-transition-progress

npm install react-transition-progress framer-motion
Enter fullscreen mode Exit fullscreen mode

Internally they are using framer motion for animations, which I found completely overkill for it's purpose.

Add the following to the layout.tsx

<ProgressBarProvider>
 <ProgressBar className={css({
    position: 'absolute',
    top: '0',
    height: '5px',
    backgroundColor: 'black',
    borderRightRadius: '100',
    boxShadow: '0 10px 15px -3px black, 0 4px 6px -2px black',
})} />
         {children}
 </ProgressBarProvider>
Enter fullscreen mode Exit fullscreen mode

For styling I'm using panda css, just change it to whatever you are using

Now change all your router.push statements into something like

// import {startTransition} from "react"
// import { useProgress } from "react-transition-progress";
// const startProgress = useProgress();
...
 startTransition(async () => {
            startProgress();
            router.push("/")
          });
...
Enter fullscreen mode Exit fullscreen mode

Also instead of next's default Link component use Link from react-transition-progress

react Article's
30 articles in total
React is a JavaScript library for building user interfaces, enabling developers to create reusable components and dynamic web applications.
Favicon
Redux Middleware সম্পর্কে বিস্তারিত আলোচনা
Favicon
POST ABOUT AI'S INCREASING INFLUENCE IN CODING
Favicon
[Boost]
Favicon
🌟 A New Adventure Begins! 🛵🍕
Favicon
From Heist Strategy to React State: How data flows between components
Favicon
Understanding React's useState with Callback Functions: A Deep Dive
Favicon
From Chaos to Clarity: Formatting React Code for a Clean and Readable Codebase
Favicon
Creating a react game on AWS
Favicon
Refactoring React: Taming Chaos, One Component at a Time
Favicon
The Magic of useCallback ✨
Favicon
Show a loading screen when changing pages in Next.js App router
Favicon
How to improve the Frontend part of the project using one button as an example :))))
Favicon
Open-Source TailwindCSS React Color Picker - Zero Dependencies! Perfect for Next.js Projects!
Favicon
Introducing EAS Hosting: Simplified deployment for modern React apps
Favicon
Understanding React's useEffect and Event Listeners: A Deep Dive
Favicon
Recreating the Interswitch Homepage with React and TailwindCSS.
Favicon
How to receive data in form from another component
Favicon
Unlocking the Secrets of React Context: Power, Pitfalls, and Performance
Favicon
"Starting My React Journey"
Favicon
Open-Source React Icon Picker: Lightweight, Customizable, and Built with ShadCN, TailwindCSS. Perfect for Next.js Projects!
Favicon
Dynamically Render Components Based on Configuration
Favicon
Conquer Breakpoints with React's useBreakpoints Hook
Favicon
Using React as Static Files in a Django Application: Step-by-Step Guide
Favicon
Don't copy/paste code you don't understand
Favicon
All-APIs.com: The Ultimate Free REST API Platform for Developers
Favicon
Form-based Dataverse Web Resources with React, Typescript and FluentUI - Part 2
Favicon
Level Up React : Deep Dive into React Elements
Favicon
Building Production-Grade Web Applications with Supabase – Part 1
Favicon
Transform Your Web Development Workflow with These JavaScript Giants
Favicon
Building High-Performance React Native Apps[Tips for Developers]

Featured ones: