Setting up a loader when changing a Nextjs page

Have you ever been on a site where you click on a link and nothing happens? Or perhaps you've wondered whether your request was actually received? This is where having a loader comes in. In this article, we'll explain why it's important to have a loader on your website, how to set it up in your Nextjs project and create a function that will display the loader until the page is loaded. If you want to improve the user experience on your website and reassure your visitors, this article is for you.
•
Updated on August 14, 2026
•
3 min read
Believemy logo

Why Implement a Loader?

Implementing a loader allows the user to understand what is happening on the site. Indeed, it would be unfortunate if nothing happened on our page... The loader communicates with the user, showing them that we have successfully received their request.

 

Prerequisites

Implementing this loader will be done in the Layout of your NextJS website. Therefore, you must have created a Layout.js file (or an equivalent name) that will serve as the base for your pages in your project.

 

What is the Layout?

The layout is a component that will be present throughout the website. Its JSX and CSS code will be applied to all pages of the site.

Typically, we include in the layout the base of each of our pages: the head tags, CSS styles, as well as headers and footers.

 

Creating the Loader

We will create a function that checks if the URL changes. If it does, we change the state using useState to activate or deactivate the loader by setting it to true. In this case, we return JSX code that will be displayed while the new page is loading.

In the following case, we simply write a loading text.

1. We start by writing a function where we call the router and create the loading state:

JAVASCRIPT
function Loading() {
        const router = useRouter();

        const [loading, setLoading] = useState(false);
}

 

2. We add a useEffect that will check if the URL changes.

JAVASCRIPT
useEffect(() => {
            const handleStart = (url) => (url !== router.asPath) && setLoading(true);
            const handleComplete = (url) => (url === router.asPath) && setLoading(false);

            router.events.on('routeChangeStart', handleStart)
            router.events.on('routeChangeComplete', handleComplete)
            router.events.on('routeChangeError', handleComplete)

            return () => {
                router.events.off('routeChangeStart', handleStart)
                router.events.off('routeChangeComplete', handleComplete)
                router.events.off('routeChangeError', handleComplete)
            }
        })

When the page change starts, we call the handleStart function, which sets the loading state to TRUE.
When it completes or fails, we call the handleComplete function, which sets the loading state to FALSE.

 

3. Finally, we return JSX.

JAVASCRIPT
return loading && (
            <>
                Loading...
            </>
);

This JSX is the content that is displayed during the page transition.

 

Final Code

JAVASCRIPT
function Loading() {
        const router = useRouter();

        const [loading, setLoading] = useState(false);


        useEffect(() => {
            const handleStart = (url) => (url !== router.asPath) && setLoading(true);
            const handleComplete = (url) => (url === router.asPath) && setLoading(false);

            router.events.on('routeChangeStart', handleStart)
            router.events.on('routeChangeComplete', handleComplete)
            router.events.on('routeChangeError', handleComplete)

            return () => {
                router.events.off('routeChangeStart', handleStart)
                router.events.off('routeChangeComplete', handleComplete)
                router.events.off('routeChangeError', handleComplete)
            }
        })

        return loading && (
            <>
                Loading...
            </>);
    }

Next, we need to add the <Loading /> tag in the JSX of the component. This allows us to call the previously created function. The loader will thus appear exactly where you place the tag.

For example - Layout.js:

JAVASCRIPT
// Component
import Loader from "loader.js";

export default function Layout(props) {
    return (
        <>
            {/* All your components */}
            <Loader />
        </>
    )
}

 

Resources

Category: Developers

Share this article

Want to help us? Share this article on your networks or even better: on your site, in an article or in your newsletter.

Believemy logo
Comments (0)

You need to be logged in to comment on this article: log in or sign up.

Read next

Implementing over-request revalidation with NextJS

If you're a web developer, you probably know how important it is to have a fast, responsive site to deliver an optimal user experience. In this article, we're going to show you how to set up on-demand revalidation with NextJS, a feature that refreshes your site's data only when necessary. Using this method, you can save resources and improve the performance of your website, while offering up-to-date data to your users.

December 9, 2024

How to use Server Actions with NextJS

Discover why and how NextJS Server Actions improve performance on both Client Components and Server Components, and learn how to use them in your projects.

December 9, 2024

Creating a skeleton with JavaScript

Let's find out together how to create a loading effect on our elements during an AJAX call, for example!

December 9, 2024

How do I add a favicon with NextJS?

Learn how to easily add a favicon to your NextJS project with this detailed guide and also find out how to generate a favicon on demand using ImageResponse.

December 6, 2024

What's new in NextJS 15

NextJS 15 was presented to the world on October 24, 2024 in San Francisco. Here's everything we know about NextJS 15's powerful new features!

December 5, 2024

How to use localStorage in a NextJs or React application

The aim of this article is to teach you how to use localStorage in a Next.JS or React application. Indeed, there are a few differences with the normal use, in a vanilla application.

December 11, 2024

Why you should learn NextJS in 2025: all the benefits!

Explore why Next.js is a must-have framework for developers in 2025. Learn about its benefits, unique features and why the job market is increasingly adopting it.

December 6, 2024

Understanding .env files with ReactJS

Find out how to really use the .env file with React or NextJS so you never make a mistake again.

December 9, 2024

Become the solopreneur you dream of being.

First project, career change, business launch: access our courses, practice on real-world cases, and move forward with clear guidance.