-
-
Notifications
You must be signed in to change notification settings - Fork 315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[LiveComponent] deferred/lazy loading #994
Comments
A bit of a DX brainstorm:
I'm sort of leaning towards a combination of 2 and 3 (opposed to livewire's 1 and 2): <twig:MyComponent data-defer="true" /> {# MyComponent.html.twig #}
<div {{ attributes }}>
{% if isInitializing %} {# new attribute provided by live components #}
Loading... (spinner or maybe a cool animated placeholder skeleton)
{% else %}
... expensive operation ...
{% endif %}
</div> On the first render, At least one problem with this idea: forcing the component to be lazy in the component template ( I'm very open to other ideas. I was hoping to leverage the current loading state system but I do not think this is possible. // cc @WebMamba, @sneakyvv any interest in this feature? I'd love some thoughts. |
One thing I like about the Livewire way of doing things is that it doesn’t add any new concepts: you create an action, then the only magic is that you ask the action to be automatically executed. With that in mind, what about something like: <div {{ attributes.defaults({
'data-action': 'appear->live#action',
'data-action-name': 'loadPosts',
}) }}>
<div> Or, you should be able to equally do this from the outside: <twig:MyComponent data-action="appear->live#action" data-action-name="loadPosts" /> This takes the normal "action-calling" syntax + Stimulus's normal If you want to simply "load immediately on page load", then instead of <twig:MyComponent data-action="live:connect->live#action" data-action-name="loadPosts" /> |
Good points. Let's keep it similar to livewire. What about adding a trait LazyTrait
{
public bool $isLoaded = false;
#[LiveAction]
public function load(): void
{
$this->isLoaded = true;
}
} {# MyComponent.html.twig #}
<div {{ attributes }}>
{% if not isLoaded %} {# from the trait #}
Loading... (spinner or maybe a cool animated placeholder skeleton)
{% else %}
... expensive operation ...
{% endif %}
</div> Calling: <twig:MyComponent data-action="appear->live#action" data-action-name="load" /> Maybe we could create a shortcut? <twig:MyComponent data-lazy /> |
I like all of this - I'm definitely not against having a shortcut, like In #996, you mentioned:
With this latest iteration, "lazy loading" isn't something the component would be aware of. There would be a convention (via the trait +
|
Hum I am not really agree here. Since the laziness of a component is a template concern, only the template should know that the component is lazy. My proposal is as follow: <twig:MyComponent data-lazy /> You only have to set this data-lazy attribute, if the data-lazy attribute is set the mount method will be called by ajax when the compose is visible, if not the mount method is call as before. |
@WebMamba I think this is valid, but a separate level of laziness. Here are the 2 cases: A) (existing idea): the component renders, but it has a flag to avoid doing something heavy. That flag changes on load, and then you do the heavy stuff. An advantage of this is that you can easily control the "loading" state - e.g. "Loading...". B) (your idea): the ENTIRE component is lazy - this is like a lazy |
I like all the ideas here, but I'm wondering if this is taking the (not yet accepted/merged) live embedded components into account. (#913). |
Btw, Livewire 3's lazy feature is like @WebMamba suggested :)
The key thing is that: the component is NOT mounted initially. As I mentioned, this means that you actually need to dehydrate the input props, which is not something that's done anywhere else. But, Livewire does this. Likely, we would need to require the input props to be scalars so that we can just serialize them easily. I think that Livewire does NOT require this, but that means they leak code info (e.g. prop
My guess is that this won't be impacted by your work over there.
Not sure what you mean here. |
I was looking at the code of the LiveRenderer and... Would it be possible to use an intermediate "proxy/flyweight" ? Twig -> ProxyComponent -> LongTaskComponent The "ProxyComponent" would store the data (in backend, file, session... anywhere BUT in the front space) then after some time it would then transmit data to the LongTaskComponent .. I mean it could fullfill some of the cases you discussed no ? |
This has been fixed for a while. |
From #102
Livewire's docs for this feature
The text was updated successfully, but these errors were encountered: