In this episode, we tackle the challenge of showing notifications automatically when using Inertia.js in a Vue app. We start by looking at the basic way of handling notifications using defineProps
and a watcher, which works but quickly gets messy if you want to display notifications with a library like Toast. You'd need to watch the notification prop in every relevant component, and that just isn't scalable or DRY.
So, instead, we look at a better way: creating a global Vue plugin. This way, we don’t have to sprinkle notification logic throughout every page or component — we handle it once for the entire app! We set up a new plugin in our project, register it in app.js
, and make it listen for Inertia's router changes. That means each time navigation happens (like after a form is submitted), our plugin checks for notifications in a centralized spot.
By using Inertia's usePage
composable, our plugin can access the page props globally and grab notifications out automatically. At this point, you could easily show a toast message or notification wherever you want, all from this one plugin.
Next time, we'll pull in a real toast notification library and wire it up, but now you’ve got a clean, global foundation for reacting to notification messages anywhere in your app!