In this episode, we dive into how shared data works in Inertia.js applications and why it's both super useful and something you need to handle with care.
First, we look at what shared data actually means: basically, it's any data that's available to all your Vue (or other frontend) components via Inertia, like the current user or global errors. We peek into the HandleInertiaRequests
middleware to see how this data gets added to every request via the share
method. You’ll see things like auth
(for user info), errors
, and even things like Ziggy (which we'll cover later!).
Next, we try adding our own example—creating a simple greeting message that's now available everywhere in the app just by putting it in share
. We explore how to access this shared data in your components using the page
prop, and also how to get to it from your script section with the usePage
composable when the page
variable isn't available.
We then demo a real-world use case where you might want the logged-in user's name directly in a form by grabbing it out of the globally shared auth
object.
But here comes the caution: just because you can share data everywhere doesn't mean you should. We show how you could, for example, share all posts globally—which works, but is usually a bad idea for performance and good sense. User data is a good choice for global sharing, but big collections like posts are usually not.
To wrap up, we talk about other situations where global shared data is actually useful, like when you need a list of topics in multiple places. It's all about balance!
By the end of this video, you'll know how to share data globally with Inertia, access it wherever you need, and have a good sense of when to use this feature wisely.