Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Max100's avatar

Does Intertia have something like beforeRouteLeave() in vue router?

In a Breeze/Vue app using Inertia I want to guard against unsaved changes when the user clicks to another route on the menu.

In another post, someone suggested using the beforeRouteLeave() guard, but that's a feature of Vue router. Does Inertia have a similar function?

I've been using the Vue onBeforeUnmount() event, which works fine but it's too late to display an app modal and so requires using javascript confirm() instead.

Not sure if beforeRouteLeave() gets called any sooner than that, so it may be a moot point anyway, but I was just wondering if Inertia has a similar feature.

0 likes
8 replies
LaryAI's avatar
Level 58

Inertia.js does not have a built-in equivalent to Vue Router's beforeRouteLeave navigation guard. However, you can achieve similar functionality by using Inertia's event system, specifically by listening to the navigate event.

Here's how you can set up a guard to check for unsaved changes before navigating away from a route in an Inertia.js/Vue application:

  1. Setup a reactive state for tracking changes: You'll need a way to track whether there have been changes in your component that haven't been saved.

  2. Listen for Inertia's navigate event: This event is triggered when a navigation is initiated.

  3. Conditionally cancel navigation: If there are unsaved changes, you can show a confirmation dialog. Based on the user's response, you can either proceed with or cancel the navigation.

Here's an example implementation:

<template>
  <div>
    <input v-model="formData.name" @input="changesMade = true" />
    <!-- Your form and other inputs -->
  </div>
</template>

<script>
import { Inertia } from '@inertiajs/inertia'

export default {
  data() {
    return {
      formData: {
        name: '',
      },
      changesMade: false
    }
  },
  mounted() {
    window.addEventListener('beforeunload', this.beforeWindowUnload)
    Inertia.on('navigate', this.handleBeforeRouteLeave)
  },
  beforeUnmount() {
    window.removeEventListener('beforeunload', this.beforeWindowUnload)
    Inertia.off('navigate', this.handleBeforeRouteLeave)
  },
  methods: {
    beforeWindowUnload(event) {
      if (this.changesMade) {
        event.preventDefault()
        event.returnValue = ''
      }
    },
    handleBeforeRouteLeave(event) {
      if (this.changesMade && !confirm('You have unsaved changes. Are you sure you want to leave?')) {
        event.preventDefault()
      }
    }
  }
}
</script>

Explanation:

  • Data Properties:

    • formData: Holds the form data.
    • changesMade: A boolean to track if changes have been made.
  • Lifecycle Hooks:

    • mounted: Adds event listeners when the component is mounted.
    • beforeUnmount: Removes event listeners when the component is about to unmount.
  • Methods:

    • beforeWindowUnload: Handles the browser's beforeunload event, which is triggered when the page is about to be unloaded (e.g., closing a tab).
    • handleBeforeRouteLeave: This method is triggered by Inertia's navigate event. It checks if there are unsaved changes and shows a confirmation dialog.

This setup helps ensure that users are prompted when they attempt to navigate away with unsaved changes, similar to the beforeRouteLeave guard in Vue Router.

gych's avatar

Should be possible by using the onBefore callback

<Link href="your route" :onBefore="CallMethodToShowModalBeforeLeaving">Example Link</Link>

Return false if you want to cancel, return true to continue

Check this page from inertia docs: https://legacy.inertiajs.com/manual-visits

Max100's avatar

Thanks for your reply! That would probably work if the link is part of the form component, but I'm not sure how it would work if the link is not in the form component.

While the user is editing a form, if they click on a link from the top menu of the AuthenticatedLayout, I wouldn't know what the user was doing before they clicked the link or if I'd have access to that form component's state.

I think I'd need to pick up the event from the form before it starts to change, ideally with the option to cancel the change. That sounds like what beforeLeaveRoute() might do, but I'm not sure and I'm not using vue router.

I'll experiment to see what I can do with onBefore. Thanks again for your reply!

gych's avatar
gych
Best Answer
Level 29

@Max100 You can use Inertia shared props to track if the form has been edited.

Once the user edits a value in the form set a shared prop isEditingForm to true. When the user clicks the link first check if the value of that shared prop is true or false via the onBefore callback.

Max100's avatar

@gych That's an interesting approach. I'd also need to track which form was edited, but it seems like that would work. I'm going to explore shared props further and try to do it this way. Thanks for your help!

gych's avatar

@Max100 No problem :) If you've more questions don't hesitate to reach out

Ben Taylor's avatar

An alternative way to handle something like this is storing the form answers in local storage. Use the on change event (or something like that) on each input to update local storage with the latest form answers. When the user visits the form, prepopulate the form with answers from the local storage. You could delete it on a successful form save.

Max100's avatar

@Ben Taylor Thanks for your reply! I haven't used local storage before, but I'm going to learn more about it.

I'd be concerned if the user switches away from unsaved changes without ever returning to the page for the values to be reloaded, but this could be addressed. I'll look into using local storage for these types of situations. Thanks again!

Please or to participate in this conversation.