Skip to content

Instantly share code, notes, and snippets.

@Rubrasum
Last active February 28, 2025 03:57
Show Gist options
  • Select an option

  • Save Rubrasum/428ef7c641971e197db0bceb7914186b to your computer and use it in GitHub Desktop.

Select an option

Save Rubrasum/428ef7c641971e197db0bceb7914186b to your computer and use it in GitHub Desktop.
Inertia Share with a condition to check for inertia load or regular site load
// Give me a better way of describing the two different loads. full request vs inertia request? idk
// Here is my original share in a service provider
public function boot(SidebarService $sidebar): void
{
// Share sidebar items with Inertia for global access
Inertia::share('sidebarItems', $sidebar->getItems());
}
// Updated fix for lazy loading
public function boot(SidebarService $sidebar): void
{
// Share sidebar items with Inertia for global access
Inertia::share('sidebarItems', function () use ($sidebar) {
if (request()->header('X-Inertia')) {
return null; // Skip on navigation
}
return Cache::remember('sidebar_items', 3600, fn () => $sidebar->getItems());
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment