Improving Web Application Performance with LoadingUI and Suspense in React and NextJs
Quick Intro
React and NextJs remains a robust framework for building rich user centric application In 2024. While various frameworks like Vue and Angular have been used to develop web applications, React has proven to be a game-changer and remains one of the best libraries for web application development.
React is a versatile library that can be used to build various types of applications. However, it is recommended to use a framework like Next.js, which is built on top of React and offers additional features for developing web applications.
If you’re not familiar with NextJS, You can refer to the documentation or enrol in this comprehensive Udemy course covering TypeScript, React, and NextJS that equips you with all the knowledge and skills necessary to become versatile with React and Next.
So let’s get to the nitty-gritty of Loading UI and Suspense in NextJs which is an incredible feature that improves the performance of web applications
Loading UI
Loading UI, as the name suggests, refers to displaying a loading ui such as skeletons, gifs, or text — on a web page while the original content is still loading. Once the content is ready, the page is displayed, and the loading UI disappears. This approach enhances user experience by informing users that content is being loaded and they need to wait a few seconds to see it.
Let’s experiment with this in VSCode
Step 1
Open visual studio code and create a folder named “training” and ensure that folder is open is vscode. Ensure you also have NodeJs installed version 18.17 or later
Step 2
Run this command in the terminal to get the NextJs app bootstrapped and ensure you are on the training directory and just press enter when prompted.
npx create-next-app@latest
Need to install the following packages:
create-next-app@14.2.3 ok to proceed? (y) y
What is your project named? … my-app
Would you like to use TypeScript? … No / Yes
Would you like to use ESLint? … No / Yes
Would you like to use Tailwind CSS? … No / Yes
Would you like to use 'src/' directory? … No / Yes
Would you like to use App Router? (recommended) No / Yes
Would you like to customize the default import alias (@/*)? … No / Yes Creating a new Next.js app in /Users/charles/ReactApps/traning/my-app.Step 3
Run the NextJs application
cd my-app
npm run devThis will open the application on localhost port 3000 which can be accessible in the browser.
Step 4
In the app folder, create a nested folder named “learning” and a page.tsx file inside the learning route. In NextJs routes are configured through folders in the app directory and route can have a default page.tsx file which is the index page.
Navigate to the url localhost:3000/learning and the page will be displayed
Step 5
Now let’s create the loading ui starting by creating a loading.tsx in the learning route folder. By convention when NextJs sees the loading.tsx file in the route, it automatically knows it is a loader and the content of that file will be displayed while the original content is loading. So in order to see this in action, let simulate a delay and show a loading ui
The loading.tsx file just displays a loading italic text
Next, let’s simulate a delay for 5 seconds in the default learning/page.tsx so we can see loader in action
Now, save and refresh the browser, you will see the loading ui content being renders within 5 seconds before the original content.
Summary
We’ve seen how to add a loading UI to the learning route. To enable the loading UI on every page within the application, move the loading.tsx file to the app route. This ensures that every page displays the loading UI before rendering the original content.
Get Charles’s stories in your inbox
Join Medium for free to get updates from this writer.
Interesting, right? Yes! However, one drawback of this approach is that it applies only to the entire page. If you need to show a loader only for a specific section, like a div displaying football match fixtures, this method is insufficient. This is where Suspense comes in to solve the problem.
Suspense
Suspense breaks down page into chunks by wrapping the content or part of the ui with suspense. By using this approach, parts of the page can be rendered while some keeps loading. Therefore, you dont have to wait for the entire page to load.
Let’s go through the steps to achieve this right away!
Step 1
Create a country.tsx component in the app route that will display the name of a country from a restful service. we are going to use fetch api to achieve this.
Step 2
Create a folder named “learn” in the app route folder and a page.tsx inside the learn folder and import the country component
When you navigate to the /learn url , you should see the country name “Germany” displayed in the browser.
The goal we are trying to achieve is to wrap the Country component in a suspense so a loader is shown while the original content is fetched from the server because this may be a time consuming operation and the other content that is beneath the Country component is just a static text.
Step 3
Now let’s wrap the Country component with a suspense by importing the suspense library from react
Now when you refresh the page, you will notice the loading country… text is shown before while the original content is being fetched from the server. The benefit of using suspense is it can be called multiple time in the application in sections that are time consuming.
You can use a loading ui together with a suspense in a page. The loading ui is shown when the page is loading while the suspense is show when a section in the page is still loading before it becomes available thus improving the performance of the web application.
Conclusion
Loading ui and suspense is a crucial aspect we should not omit when building React applications because it improves the performance of the application and user experience.
For a better understanding, here is a YouTube video explaining loading UI and Suspense in detail.
If you are not familiar with NextJs or still a React newbie, enrol for this powerful Udemy course that covers both Typescript, React and NextJs. This course equips you with everything needed to become a React and NextJs guru.
Hope you enjoyed this article!
Best of luck!









