Laravel + Inertia Once Props: Shared Data Magic for Faster SPAs (Taylor’s Favorite)

Single Page Applications (SPAs) are everywhere. They deliver smooth, app-like experiences on the web, but they also come with challenges: data fetching, performance bottlenecks, and redundant requests.

If you’re building with Laravel + Inertia.js, you already know the joy of combining Laravel’s backend power with Inertia’s frontend simplicity. But there’s one feature that makes this duo even more magical: Once Props.

Taylor Otwell himself has called it one of his favorite Inertia features — and for good reason. Once Props solve a subtle but critical problem in SPA development: how to share data across pages without re-fetching it every time.


🚀 The Problem: Redundant Data Fetching

Imagine you’re building a SaaS dashboard. Every page needs access to:

  • The authenticated user’s profile
  • The current team or tenant info
  • Global settings or feature flags

Without Once Props, you’d end up fetching this data on every page visit. That means extra queries, slower responses, and wasted resources.

SPAs thrive on speed, so repeating the same queries across multiple pages is a performance killer.


🔑 The Solution: Inertia Once Props

Once Props let you define data that should be fetched only once and then shared across all subsequent page visits.

Think of it as a global data cache for your SPA. Instead of hitting the database or API repeatedly, Inertia remembers the data and reuses it until you explicitly refresh it.

Example: Sharing Auth User Data

In your Laravel AppServiceProvider, you might define shared props like this:

use Inertia\Inertia;

public function boot()
{
    Inertia::share([
        'auth.user' => fn () => auth()->user()
            ? auth()->user()->only('id', 'name', 'email')
            : null,
    ]);
}

With Once Props, you can mark this data to load only once:

Inertia::share([
    'auth.user' => Inertia::once(fn () => auth()->user()
        ? auth()->user()->only('id', 'name', 'email')
        : null),
]);

Now, the user data is fetched once and shared across all pages — no repeated queries.


⚡ Benefits of Once Props

  • Performance Boost: Reduce redundant queries and API calls.
  • Consistency: Ensure shared data (like user info) stays consistent across pages.
  • Developer Happiness: Less boilerplate, fewer headaches managing global state.
  • SPA Magic: Pages feel faster because shared data is instantly available.

🛠️ Real-World Use Cases

  • Authenticated User: Share user profile data across the entire app.
  • Tenant/Team Context: In multi-tenant SaaS, share the current team info once.
  • Global Settings: Feature flags, app config, or branding details.
  • Navigation Menus: Build menus once and reuse them everywhere.

🌐 Refreshing Once Props

What if the data changes? For example, a user updates their profile.

Inertia provides a way to refresh Once Props when needed. You can trigger a reload to fetch the latest data, ensuring your SPA stays up-to-date without losing the performance benefits.


⚖️ Why Taylor Loves It

Taylor Otwell has highlighted Once Props as one of his favorite Inertia features because it embodies Laravel’s philosophy: developer experience + performance.

Instead of forcing developers to manage global state manually or rely on heavy frontend frameworks, Once Props give you a simple, elegant solution right inside Laravel + Inertia.


🧭 Final Thoughts

Laravel + Inertia.js already feels like magic for building SPAs without the complexity of a full frontend framework. Once Props take that magic further by solving a real pain point: shared data across pages.

By fetching data once and reusing it, you get:

  • Faster apps
  • Cleaner code
  • Happier developers

If you’re building a SaaS or any SPA with Laravel + Inertia, Once Props aren’t just a nice-to-have — they’re a must-use feature.

Leave a Reply

Your email address will not be published. Required fields are marked *