Registering Listeners
To register an event listener, use therouter.on() method.
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.removeEventListener().
Cancelling Events
Some events, such asbefore, invalid, and error, 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().
router.on(), you can cancel the event by returning false from the listener.
popstate event, so preventing forward and back history visits while using Inertia.js is not possible.
Before
Thebefore event fires when a request is about to be made to the server. This is useful for intercepting visits.
Start
Thestart event fires when a request to the server has started. This is useful for displaying loading indicators.
start event is not cancelable.
Progress
Theprogress event fires as progress increments during file uploads.
progress event is not cancelable.
Success
Thesuccess event fires on successful page visits, unless validation errors are present. However, this does not include history visits.
success event is not cancelable.
Error
Theerror event fires when validation errors are present on “successful” page visits.
error event is not cancelable.
Invalid
Theinvalid 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.
invalid event to prevent Inertia from showing the non-Inertia response modal.
Exception
Theexception event fires on unexpected XHR errors such as network interruptions. In addition, this event fires for errors generated when resolving page components.
exception event to prevent the error from being thrown.
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
Thefinish event fires after an XHR request has completed for both “successful” and “unsuccessful” responses. This event is useful for hiding loading indicators.
finish event is not cancelable.
Navigate
Thenavigate event fires on successful page visits, as well as when navigating through history.
navigate event is not cancelable.