Skip to main content
Inertia provides an event system that allows you to “hook into” the various lifecycle events of the library.

Registering Listeners

To register an event listener, use the router.on() method.
Under the hood, Inertia uses native browser events, so you can also interact with Inertia events using the typical event methods you may already be familiar with - just be sure to prepend inertia: to the event name.

Removing Listeners

When you register an event listener, Inertia automatically returns a callback that can be invoked to remove the event listener.
Combined with hooks, you can automatically remove the event listener when components unmount.
Alternatively, if you’re using native browser events, you can remove the event listener using removeEventListener().

One-time Listeners

You may register a listener that fires only once using the router.once() method. The listener is automatically removed after the first invocation.
Like router.on(), this method returns a callback you may invoke to remove the listener before it fires.

Cancelling Events

Some events, such as before, networkError, httpException, and location, support cancellation, allowing you to prevent Inertia’s default behavior. Just like native events, the event will be cancelled if only one event listener calls event.preventDefault().
For convenience, if you register your event listener using router.on(), you can cancel the event by returning false from the listener.
Note, browsers do not allow cancelling the native popstate event, so preventing forward and back history visits while using Inertia.js is not possible.

Before

The before event fires when a request is about to be made to the server. This is useful for intercepting visits.
The primary purpose of this event is to allow you to prevent a visit from happening.

Start

The start event fires when a request to the server has started. This is useful for displaying loading indicators.
The start event is not cancelable.

Progress

The progress event fires as progress increments during file uploads.
The progress event is not cancelable.

Success

The success event fires on successful page visits, unless validation errors are present. However, this does not include history visits.
The success event is not cancelable.

Flash

The flash event fires when flash data is received from the server. This is useful for displaying toast notifications or handling temporary data in a central location.
The flash event is not cancelable and fires on every response that carries flash data.

Error

The error event fires when validation errors are present on “successful” page visits.
The error event is not cancelable.

HTTP Exception

The httpException event fires when a non-Inertia response is received from the server, such as an HTML or vanilla JSON response. A valid Inertia response is a response that has the X-Inertia header set to true with a json payload containing the page object. This event is fired for all response types, including 200, 400, and 500 response codes.
You may cancel the httpException event to prevent Inertia from showing the non-Inertia response modal.

Network Error

The networkError event fires on unexpected XHR errors such as network interruptions. In addition, this event fires for errors generated when resolving page components.
You may cancel the networkError event to prevent the error from being thrown.
This event will not fire for XHR requests that receive 400 and 500 level responses or for non-Inertia responses, as these situations are handled in other ways by Inertia. Please consult the error handling documentation for more information.

Finish

The finish event fires after an XHR request has completed for both “successful” and “unsuccessful” responses. This event is useful for hiding loading indicators.
The finish event is not cancelable. The navigate event fires on successful page visits, as well as when navigating through history.
The navigate event is not cancelable.

Location

v3.6.0+ The location event fires whenever Inertia is about to force a full-page navigation, either because the asset version changed or because the server returned an Inertia::location() redirect. The event detail includes a versionChange boolean so you may tell an automatic asset version change apart from a manual redirect.
You may cancel the location event to take over the navigation yourself, for example to show a “new version available” banner instead of reloading.

Prefetching

The prefetching event fires when the router starts prefetching a page.
The prefetching event is not cancelable.

Prefetched

The prefetched event fires when the router has successfully prefetched a page.
The prefetched event is not cancelable.

Event Callbacks

In addition to the global events described throughout this page, Inertia also provides a number of event callbacks that fire when manually making Inertia visits.