Skip to content

Commit b4a659c

Browse files
committed
[LazyImage] Webperfs and LCP considerations
1 parent bdb5bc3 commit b4a659c

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

src/LazyImage/doc/index.rst

+39-1
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,42 @@ Then in your template, add your controller to the HTML attribute:
205205
controller so that it is executed before and can listen on the
206206
``lazy-image:connect`` event properly.
207207

208+
Largest Contentful Paint (LCP) and Web performance considerations
209+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
210+
211+
The `Largest Contentful Paint (LCP)`_ is a key metric for web performance, it measures the time
212+
it takes for the largest image or text block to be rendered on the page, and should be less
213+
than 2.5 seconds.
214+
It's part of the `Core Web Vitals`_ and is used by Google to evaluate the user experience
215+
of a website, and impacts the Search ranking.
216+
217+
Using the Symfony UX LazyImage for your LCP image can be a good idea at first,
218+
but in the reality it will lower your LCP score because:
219+
- `The progressive loading (through blurhash) is not taken into account in the LCP calculation <https://github.com/w3c/largest-contentful-paint/issues/71>`_
220+
- event if you eagerly load the LazyImage Stimulus controller, a small delay will be added to the
221+
LCP calculation
222+
- if you `didn't preload the image <https://symfony.com/doc/current/web_link.html>`_, the browser
223+
will waits for the Stimulus controller to load the image, which add another delay to the LCP
224+
calculation
225+
226+
A solution is to not use the Stimulus controller for the LCP image, but use ``src`` and ``style``
227+
attributes instead, and preload the image as well:
228+
229+
.. code-block:: html+twig
230+
231+
<img
232+
src="{{ preload(asset('image/large.png'), { as: 'image', fetchpriority: 'high' }) }}"
233+
style="background-image: url('{{ data_uri_thumbnail('public/image/large.png', 20, 15) }}')"
234+
fetchpriority="high"
235+
236+
{# Using BlurHash, the size is required #}
237+
width="200"
238+
height="150"
239+
/>
240+
241+
This way, the browser will display the BlurHash image as soon as possible, and will load the HD
242+
image at the same time, without waiting for the Stimulus controller to be loaded.
243+
208244
Backward Compatibility promise
209245
------------------------------
210246

@@ -217,4 +253,6 @@ https://symfony.com/doc/current/contributing/code/bc.html
217253
.. _`StimulusBundle`: https://symfony.com/bundles/StimulusBundle/current/index.html
218254
.. _StimulusBundle configured in your app: https://symfony.com/bundles/StimulusBundle/current/index.html
219255
.. _`file_get_contents`: https://www.php.net/manual/en/function.file-get-contents.php
220-
.. _`Flysystem`: https://flysystem.thephpleague.com
256+
.. _`Flysystem`: https://flysystem.thephpleague.com
257+
.. _`Largest Contentful Paint (LCP)`: https://web.dev/lcp/
258+
.. _`Core Web Vitals`: https://web.dev/vitals/

0 commit comments

Comments
 (0)