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().

Cancelling Events

Some events, such as before, exception, and invalid, 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.

Error

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

Invalid

The invalid 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 500response codes.
You may cancel the invalid event to prevent Inertia from showing the non-Inertia response modal.

Exception

The exception 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 exception 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.

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.