Default
Inertia’s default progress indicator is a light-weight wrapper around the NProgress library. You can customize it via theprogress property of the createInertiaApp() function.
progress property to false.
Programmatic Access
When you need to control the progress indicator outside of Inertia requests, for example, when making requests with Axios or other libraries, you can use Inertia’s progress methods directly.hide() and reveal() methods work together to prevent conflicts when separate parts of your code need to control progress visibility. Each hide() call increments an internal counter, while reveal() decrements it. The progress bar only appears when this counter reaches zero.
However, reveal() accepts an optional force parameter that bypasses this counter. Inertia uses this pattern internally to hide progress during prefetching while ensuring it appears for actual navigation.
progress: false in createInertiaApp(), these programmatic methods will not work.
Custom
It’s also possible to setup your own custom page loading indicators using Inertia events. Let’s explore how to do this using the NProgress library as an example. First, disable Inertia’s default loading indicator.NProgress and the Inertia router into your application.
start event listener. We’ll use this listener to show the progress bar when a new Inertia visit begins.
finish event listener to hide the progress bar when the page visit finishes.
Handling Cancelled Visits
While this custom progress implementation works great for page visits that finish properly, it would be nice to handle cancelled visits as well. First, for interrupted visits (those that get cancelled as a result of a new visit), the progress bar should simply be reset back to the start position. Second, for manually cancelled visits, the progress bar should be immediately removed from the page. We can accomplish this by inspecting theevent.detail.visit object that’s provided to the finish event.
File Upload Progress
Let’s take this a step further. When files are being uploaded, it would be great to update the loading indicator to reflect the upload progress. This can be done using theprogress event.
Loading Indicator Delay
The last thing we’re going to implement is a loading indicator delay. It’s often preferable to delay showing the loading indicator until a request has taken longer than 250-500 milliseconds. This prevents the loading indicator from appearing constantly on quick page visits, which can be visually distracting. To implement the delay behavior, we’ll use thesetTimeout and clearTimeout functions. Let’s start by defining a variable to keep track of the timeout.
start event listener to start a new timeout that will show the progress bar after 250 milliseconds.
finish event listener to clear any existing timeouts in the event that the page visit finishes before the timeout does.
finish event listener, we need to determine if the progress bar has actually started displaying progress, otherwise we’ll inadvertently cause it to show before the timeout has finished.
progress event listener.
Complete Example
For convenience, here is the full source code of the final version of our custom loading indicator.Visit Options
In addition to these configurations, Inertia.js provides two visit options to control the loading indicator on a per-request basis:showProgress and async. These options offer greater control over how Inertia.js handles asynchronous requests and manages progress indicators.
Showprogress
TheshowProgress option provides fine-grained control over the visibility of the loading indicator during requests.
Async
Theasync option allows you to perform asynchronous requests without displaying the default progress indicator. It can be used in combination with the showProgress option.