In today's fast-paced online world, website loading speed is crucial for user experience and search engine rankings.If your website content contains a large number of images, optimizing their loading method will be a key step in improving website performance.Image lazy loading (Lazy Load) is a highly efficient solution.

Why is image lazy loading so important?

Website images can enrich content and enhance visual appeal, but they are also often the main reason for slow page loading.When a page carries a large number of high-resolution images, the browser needs to download all these image resources during the initial loading, which undoubtedly consumes a lot of time and bandwidth.The user may encounter a long wait, even choosing to leave before the page is fully loaded.Search engines consider page loading speed to be an indispensable indicator when evaluating website quality and ranking.Therefore, it is of great significance to manage image loading reasonably, especially to enable lazy loading of images, which is beneficial for optimizing user experience, reducing bounce rate, and improving website SEO performance.

What is image lazy loading?

In simple terms, lazy loading of images is a 'on-demand loading' strategy.The core idea is to defer loading non-critical resources. When a user first visits a page containing a large number of images, only the images within the visible area of the current screen will load immediately, while those located below the page and not yet entering the user's viewport will pause loading.These images will start to be requested and displayed when the user scrolls down the page and are about to enter the visible area.This not only greatly shortens the initial loading time of the page, but also saves unnecessary bandwidth consumption, allowing users to see the content they really care about faster.

How to implement lazy loading of images in the article content of AnQi CMS?

Anqi CMS was designed with performance optimization needs in mind and provides convenient support for lazy loading of images in content.It enables lazy loading of images in the main content of the article with flexible use of template tags.

To achieve this function, we mainly make use ofarchiveDetailA special parameter in the tag. In the Anqi CMS template system, for example, in the template file of the article detail page (usually{模型table}/detail.html), we usearchiveDetailUse the tag to retrieve and display the main content of the article, that isContentfield.

To make the images in the article content lazy load, you need toarchiveDetailAdd alazyparameter. This parameter will indicate to the system, during renderingContentWhen a field<img>label'ssrcreplaces itslazynew attribute specified by thedata-src),and stores the original image address in this new attribute.

The specific operation method example is as follows:

{% archiveDetail articleContent with name="Content" lazy="data-src" %}{{articleContent|safe}}

In this code block:

  • {% archiveDetail articleContent with name="Content" %}This part is responsible for obtaining the article from the background:ContentAnd assign it to the template in:articleContentVariable.
  • lazy="data-src"This is the key instruction to enable lazy loading. It will traversearticleContentAll of<img>tags, setsrc="图片地址"The structure is converted todata-src="图片地址"And it may clear the originalsrcProperty (or set to a placeholder, depending on the front-end implementation).
  • {{articleContent|safe}}:|safeThe role of the filter here is to ensurearticleContentThe HTML code contained in the variable is correctly parsed and rendered by the browser, rather than being escaped as plain text.

By adding in the templatelazy="data-src"This parameter setting, Anqi CMS has prepared the foundation for the front-end lazy loading mechanism, with the following featuresdata-srcimage elements with properties.

It should be noted that merely modifying the HTML structure through templates is not enough to fully realize lazy loading.The actual lazy loading effect of images still needs the cooperation of front-end JavaScript libraries.These libraries will be responsible for monitoring the page's scroll events, when image elements enter the user's visible area, they will bedata-srcthe image address stored in the properties is reassigned tosrcProperty, thus triggering the actual loading and display of the image.

Significant advantages brought by lazy loading of images.

After enabling lazy loading of images, your website will be able to experience a multi-faceted performance boost and user experience optimization:

  • Page loading speed is significantly improved:When initially loading, the browser does not need to download all the images on the page at once, but only the images visible to the current user.This greatly reduces the time to first contentful paint (FCP) and time to interactive (TTI).
  • Optimize bandwidth and traffic consumption:Avoided loading images that the user may never scroll to and view, thereby reducing the server's bandwidth usage and also saving valuable traffic for the users visiting the website.
  • Improve user experience:Users do not have to wait long to see the main content of the page, reducing anxiety during waiting and improving overall browsing fluidity and satisfaction.
  • Helps with Search Engine Optimization (SEO):Page loading speed is an important factor in search engine ranking algorithms. Faster loading speeds help improve the visibility of websites in search results and attract more potential visitors.

Implement lazy loading suggestions

When practicing image lazy loading in Anqi CMS, to ensure the effect and avoid potential problems, you can consider the following points:

  1. Choose a lightweight and efficient front-end lazy loading JS library:There are many excellent open-source lazy loading libraries on the market, such aslazysizes/vanilla-lazyloadThey are usually small in size, have good browser compatibility, and are easy to integrate.You can choose a library that meets your project requirements and include its JS file in your website template (for example,base.html).
  2. Provide a visual placeholder for images that have not yet been loaded:Before the image is loaded, there may be blank areas on the page, which may affect the user experience.To avoid this abruptness, you can set a uniform placeholder background color, a small loading animation, or use a very low-quality (BLURRED_LOW_RES) blurred image as a transition.This is usually achieved through CSS styling or the configuration of a lazy loading JS library.
  3. Reasonably plan the above-the-fold image loading:For images located on the initial screen of the page, which users can see without scrolling, it is strongly recommended not to enable lazy loading. These images should be loaded directly through regularsrcAttribute loading to ensure that users can immediately obtain the most important visual information when they open the page.You can manually control it when writing article content, or set exclusion rules in the frontend JS logic.
  4. Focus on SEO compatibility:Modern search engines (especially Google) have the ability to execute JavaScript, and can usually identify