Skip to content

Commit e4a5897

Browse files
committed
minor #1967 [LazyImage] Webperfs and LCP considerations (Kocal)
This PR was merged into the 2.x branch. Discussion ---------- [LazyImage] Webperfs and LCP considerations | Q | A | ------------- | --- | Bug fix? | no | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Issues | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT <!-- Replace this notice by a description of your feature/bugfix. This will help reviewers and should be a good start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - For new features, provide some code snippets to help understand usage. - Features and deprecations must be submitted against branch main. - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry - Never break backward compatibility (see https://symfony.com/bc). --> Added some documentation to the LazyImage component, about LCP and webperfs considerations. tl;dr: using the LazyImage _in its initial way_ on the LCP image, will decrease your LCP, lower the user experience, and lower the ranking of your website on Google. Using the `style="background-image: url(...)"` trick solves this issue, since it allows the browser to download and render the HD image the fastest as possible, while showing the blurhash during the loading. This is something I've implemented ~1 month ago on our website wamiz.com, and did some comparaison: - Before, with the LazyImage Stimulus Controller solution, the blured image is not shown at all, and the LCP image discovered too much late: ![image](https://github.com/symfony/ux/assets/2103975/f61ae408-1e70-47ba-9b00-93fa5c85b56b) - After, with the `style="background-image: url(...)"` trick, we can see the blured image and the LCP image discovered faster: ![image](https://github.com/symfony/ux/assets/2103975/d8402267-c67b-44a8-ae0f-8eaf2eaeb35c) Commits ------- a46b1d3 [LazyImage] Webperfs and LCP considerations
2 parents 99b6359 + a46b1d3 commit e4a5897

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

src/LazyImage/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)