#Livewire load wait for previous component in foreach to load before loading the next one.

1 messages · Page 1 of 1 (latest)

vocal cypress
#

I have a livewire component that loads other livewire components in a foreach loop, is there a way to wait for each component in the loop to fully load before sending the next request to load the next component in the loop?

fallen heart
#

But the question is: why are you wanting to do that? is it for performance reasons or a particular effect you're trying to achieve? that might be better suited to a different technique

#

i wouldn't suggest this approach as it means LOTS of trips back and forth to the server which means slower and less responsive loading/pages

vocal cypress
#

Yes something like a that came to mind, just wanted to check if there was a better way to do this. The components in the loop are charts and each one loads a lot of data, so sometimes the request times out. Although i'm not even sure if this approach is correct either (since even loading them 1-1 they might still time out) .

fallen heart
#

Check out the defer loading link i just sent, we use it to load heavy pages too - essentially the full page loads (with a placeholder for the chart), then once the page itself has loaded, it sends the deferred request for the charts.

#

If you really want to do them one at a time though, the $step thing might work, but also look at just caching your chart queries so that the first load is slow but each subsequent one is faster, and you can stagger your cache expiry times so they don't all expire at once.

#

And if they're timing out, maybe look at your queries in general

#

Happy to help with query optimisation as well if you wanna talk about that.

vocal cypress
#

i already use it in the sub components, its just the data is too much and sometimes it times out. This page is accessed like 1-2 times a day at random by admins. I think i will make a command that runs every 2 hours and caches the whole thing then get it from the cache.

fallen heart
#

Yeah that sounds perfect, pre-calculation always good for heavy workloads. You could even have an artisan command that rebuilds it every 10 minutes if you want 🤷‍♂️

#

as long as your admins are happy with it being out of date by 10 mins that's perfect.

vocal cypress
#

yeap, thanks for the help, ill mark this as solved