Today, we're diving into one of Livewire's super great features: Lazy Loading
If you've been around the block with web development, you know that performance can make or break your application. Because Livewire does most rendering server-side, a request could be held up by the server. Luckily, there is a way to solve this, thanks to Livewire's Lazy Loading magic, ensuring your app remains snappy and user-friendly.
In the simplest terms, Lazy Loading in Livewire is about loading only what's necessary, when it's necessary. Imagine walking into a buffet. Instead of piling everything onto your plate at once (and risking eating too much), you take what you need, when you need it. That's Lazy Loading for you - serving up content in digestible, efficient portions.
Getting Lazy Loading up and running in Livewire is a breeze. Here's a quick guide:
Identify What to Lazy Load: Look at your components and spot elements that aren't immediately necessary and that are holding up the loading of the page. These could be comments in a blog post, or maybe things that require more heavy calculations like statistics.
Once you've identified which components you would like to lazy load, all you have to do is add the #[Lazy]
attribute.
1#[Lazy]2class WeekReport extends Component3{4 // ...5}
This will ensure this component is always lazy loaded. If you want to lazy load this component only in specific places, you can remove the #[Lazy]
attribute and simply add the lazy
argument to the component:
1<livewire:week-report :lazy="true" />
When working with full-page components, you also have the option to enable lazy loading from your route definition:
1Route::get('/dashboard', Dashboard::class)->lazy();
In all our examples, the component will be hidden until it's ready. This might be a bit odd depending on your application, given a user might see an empty screen. To address this, you can use a loading placeholder. All we need to do is add a placeholder
method to our component and return our placeholder:
1class WeeklyReport extends Component 2{ 3 public $report; 4 5 public function mount($report) 6 { 7 $this->report = $report->calculate(); // This takes a few seconds 8 } 9 10 public function placeholder()11 {12 return <<<'HTML'13 <div>14 Loading report...15 </div>16 HTML;17 }18 19 public function render()20 {21 return view('livewire.revenue');22 }23}
Your placeholder will likely be generic, but if you want, you can access any component arguments and adjust the placeholder:
1class WeeklyReport extends Component2{3 //...4 5 public function placeholder($report)6 {7 return Blade::render('<div>Loading report: {{ $report->name }}</div>', ['report' => $report]);8 }9}
If you are using generic placeholders, repeating this for every component might be cumbersome. Instead, you could define the default placeholder for all your components in the config/livewire.php
config file.
1'lazy_placeholder' => 'livewire.my-placeholder',
And don't worry, you can still override the placeholder on a component level by adding the placeholder
method.
This technique is great making your applications not only efficient but also a joy for your users. Whether it's speeding up initial page loads, reducing server strain, or enhancing overall user experience, Lazy Loading has got your back.
Implementing Lazy Loading, as we've seen, is straightforward and adaptable to your specific needs. Whether you're adding the #[Lazy] attribute directly or invoking lazy loading in your routes, you have the flexibility to choose what works best for your application. And let's not forget the charm of loading placeholders, keeping your users engaged even as the data gets ready behind the scenes.
Happy coding!