Skip to content

Commit deb7272

Browse files
committed
[LazyImage] Webperfs and LCP considerations
1 parent e762ea4 commit deb7272

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

doc/index.rst

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,41 @@ 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`_
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`_, the browser will waits for the Stimulus controller to
223+
load the image, which add another delay to the LCP calculation
224+
225+
A solution is to not use the Stimulus controller for the LCP image, but use ``src`` and ``style``
226+
attributes instead, and preload the image as well:
227+
228+
.. code-block:: html+twig
229+
230+
<img
231+
src="{{ preload(asset('image/large.png'), { as: 'image', fetchpriority: 'high' }) }}"
232+
style="background-image: url('{{ data_uri_thumbnail('public/image/large.png', 20, 15) }}')"
233+
fetchpriority="high"
234+
235+
{# Using BlurHash, the size is required #}
236+
width="200"
237+
height="150"
238+
/>
239+
240+
This way, the browser will display the BlurHash image as soon as possible, and will load the HD
241+
image at the same time, without waiting for the Stimulus controller to be loaded.
242+
208243
Backward Compatibility promise
209244
------------------------------
210245

@@ -217,4 +252,8 @@ https://symfony.com/doc/current/contributing/code/bc.html
217252
.. _`StimulusBundle`: https://symfony.com/bundles/StimulusBundle/current/index.html
218253
.. _StimulusBundle configured in your app: https://symfony.com/bundles/StimulusBundle/current/index.html
219254
.. _`file_get_contents`: https://www.php.net/manual/en/function.file-get-contents.php
220-
.. _`Flysystem`: https://flysystem.thephpleague.com
255+
.. _`Flysystem`: https://flysystem.thephpleague.com
256+
.. _`Largest Contentful Paint (LCP)`: https://web.dev/lcp/
257+
.. _`Core Web Vitals`: https://web.dev/vitals/
258+
.. _`The progressive loading (through blurhash) is not taken into account in the LCP calculation`: https://github.com/w3c/largest-contentful-paint/issues/71_
259+
.. _`didn't preload the image`: https://symfony.com/doc/current/web_link.html

0 commit comments

Comments
 (0)