Skip to content

Commit 2f9ef84

Browse files
committed
[LiveComponent] Tweaking the defer docs
1 parent e26be66 commit 2f9ef84

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

src/LiveComponent/doc/index.rst

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2220,31 +2220,49 @@ To validate only on "change", use the ``on(change)`` modifier:
22202220
class="{{ _errors.has('post.content') ? 'is-invalid' : '' }}"
22212221
>
22222222

2223-
Deferring the Loading
2224-
---------------------
2223+
Deferring / Lazy Loading Components
2224+
-----------------------------------
22252225

2226-
Certain components might be heavy to load. You can defer the loading of these components
2227-
until after the rest of the page has loaded. To do this, use the ``defer`` attribute:
2226+
.. versionadded:: 2.13.0
2227+
2228+
The ability to defer loading a component was added in Live Components 2.13.
2229+
2230+
If a component is heavy to render, you can defer rendering it until after
2231+
the page has loaded. To do this, add the ``defer`` option:
22282232

22292233
.. code-block:: twig
22302234
22312235
{{ component('SomeHeavyComponent', { defer: true }) }}
22322236
2233-
Doing so will render an empty "placeholder" tag with the live attributes. Once the ``live:connect`` event is triggered,
2234-
the component will be rendered asynchronously.
2237+
This renders an empty ``<div>`` tag, but triggers an Ajax call to render the
2238+
real component once the page has loaded.
2239+
2240+
.. note::
22352241

2236-
By default the rendered tag is a ``div``. You can change this by specifying the ``loading-tag`` attribute:
2242+
Behind the scenes, your component *is* created & mounted during the initial
2243+
page load, but it isn't rendered. So keep your heavy work to methods in
2244+
your component (e.g. ``getProducts()``) that are only called when rendering.
2245+
2246+
To add some loading text before the real component is loaded, use the
2247+
``loading-template`` option to point to a template:
22372248

22382249
.. code-block:: twig
22392250
2240-
{{ component('SomeHeavyComponent', { defer: true, loading-tag: 'span' }) }}
2251+
{{ component('SomeHeavyComponent', { defer: true, loading-template: 'spinning-wheel.html.twig' }) }}
22412252
2242-
If you need to signify that the component is loading, use the ``loading-template`` attribute.
2243-
This lets you provide a Twig template that will render inside the "placeholder" tag:
2253+
Or override the ``content`` block:
22442254

22452255
.. code-block:: twig
22462256
2247-
{{ component('SomeHeavyComponent', { defer: true, loading-template: 'spinning-wheel.html.twig' }) }}
2257+
{% component SomeHeavyComponent with { defer: true }) }}
2258+
{% block content %}Loading...{% endblock %}
2259+
{{ end_component() }}
2260+
2261+
To change the initial tag from a ``div`` to something else, use the ``loading-tag`` option:
2262+
2263+
.. code-block:: twig
2264+
2265+
{{ component('SomeHeavyComponent', { defer: true, loading-tag: 'span' }) }}
22482266
22492267
Polling
22502268
-------

0 commit comments

Comments
 (0)