unlazy Documentation

repository·main·Indexed 19 days ago

https://github.com/johannschopplich/unlazy

A universal, framework-agnostic lazy loading library leveraging native browser loading="lazy" attributes. It features BlurHash/ThumbHash support, automatic sizes attribute calculation, and LCP optimization for above-the-fold images to improve Core Web Vitals. Provides dedicated integrations for Vanilla JS, Vue, Nuxt, React, Solid, and Svelte (v5+).

Tokens
22.7K
Snippets
91
Records
123
Agent score
73%

What's inside unlazy

  1. What is unlazy?

    main

    unlazy is a universal, framework-agnostic lazy loading library designed for placeholder images. It leverages native browser APIs like loading="lazy" to provide efficient image loading.

    Key capabilities include:

    • Placeholder Support: Uses BlurHash and ThumbHash compact hash strings to decode into blurry placeholder images.
    • Flexible Decoding: Supports SSR (Server-Side Rendering) decoding or client-side decoding at runtime.
    • Auto-Sizing: Automatically calculates the sizes attribute based on display width.
    • SEO Optimization: Detects bots and crawlers to load full images immediately for indexing.
    • Broad Compatibility: Works with any framework or no framework at all. Dedicated integrations are available for Vue, React, Solid, Svelte, and Nuxt.
  2. Overview of unlazy API functions

    main

    The unlazy library provides several utility functions for managing lazy loading, placeholder generation, and image handling.

    Key functions include:

    • lazyLoad: Initializes lazy loading with placeholder generation.
    • autoSizes: Calculates the sizes attribute for responsive images without using lazy loading.
    • triggerLoad: Programmatically forces an image to load immediately.
    • createPlaceholderFromHash: Generates a PNG data URI from a hash on the client-side.
    • createPngDataUri (from unlazy/blurhash): Generates a PNG data URI from a BlurHash (optimized for SSR).
    • createPngDataUri (from unlazy/thumbhash): Generates a PNG data URI from a ThumbHash (optimized for SSR).
  3. Available unlazy integrations

    main

    unlazy provides official integrations for several frontend and meta-frameworks. Each integration provides a UnLazyImage component designed as a drop-in replacement for the native <img> element.

    Frontend Frameworks

    • Vue
    • React
    • Solid
    • Svelte

    Meta-Frameworks

    • Nuxt (Supports SSR-decoding of hash-based placeholders like BlurHash and ThumbHash)
  4. Key features of @unlazy/react

    main

    The @unlazy/react component provides several optimized image loading capabilities:

    • Native Lazy Loading: Uses the browser's native loading="lazy" attribute.
    • Hash-based Placeholders: Supports BlurHash and ThumbHash for both SSR and client-side decoding.
    • Core Web Vitals Optimization: Includes an eager-priority path for above-the-fold images and provides LCP (Largest Contentful Paint) warnings in development mode.
    • Art Direction: Supports the <picture> element with automatic sizes attribute management.
  5. What is unlazy and how does it work?

    main

    unlazy is a lazy loading library that enhances the native browser loading="lazy" attribute to improve perceived performance. It uses inlined placeholder images (like BlurHash or ThumbHash) that swap to full-quality versions as images enter the viewport.

    Core Mechanisms:

    • Attribute Swapping: It uses data-src, data-srcset, and data-sizes attributes. These are swapped to standard src, srcset, and sizes attributes once the image loads.
    • Placeholder Decoding: It decodes hash-based strings (BlurHash/ThumbHash) into PNG data URIs for initial rendering.
    • Chrome Workaround: It uses unique indexed SVG placeholders to prevent Chrome from prematurely triggering load events.
    • Auto Sizes: If data-sizes="auto" is provided, unlazy calculates the sizes attribute based on the image's display width.
  6. How unlazy handles SEO and crawlers

    main

    To ensure search engine bots and crawlers index high-quality images instead of blurry placeholders, unlazy uses an isCrawler constant to detect non-human visitors.

    When a crawler is detected via User Agent scanning or by checking for onscroll support, unlazy immediately converts data-srcset and data-src attributes into their standard srcset and src counterparts. This allows bots to see and index the full-quality image sources directly.

  7. Compare Client-Side vs Server-Side Hash Decoding

    main

    Client-Side Decoding

    • Mechanism: Uses data-blurhash or data-thumbhash attributes.
    • Pros: Placeholders are visible immediately on page load.
    • Cons: Increases client-side JavaScript bundle size because the decoding library must be included.

    Server-Side Decoding

    • Mechanism: Uses createPngDataUri to embed a PNG data URI in the src attribute.
    • Pros: Keeps client bundle smaller; ideal for minimizing JavaScript.
    • Cons: Requires server-side processing logic.
  8. How `lazyLoad` handles eager vs lazy images

    main

    The lazyLoad function follows two distinct paths based on the image's loading attribute:

    1. Eager Images (loading="eager"):

      • data-src or data-srcset are immediately swapped into src or srcset.
      • fetchpriority="high" is added if not already present.
      • If data-blurhash or data-thumbhash is present, a hash placeholder is generated immediately to provide an instant visual while the real image fetches.
      • This path is also used for crawlers to ensure search engines see the real image.
    2. Lazy Images (loading="lazy"):

      • Uses hash-based placeholders (BlurHash or ThumbHash) if data-blurhash or data-thumbhash attributes are provided.
      • Expands data-sizes="auto" into a real sizes value.
      • Relies on the browser's native loading="lazy" implementation to trigger the source swap when the image enters the viewport.

    Note: During development, unlazy will warn you if an element identified as your Largest Contentful Paint (LCP) is configured for lazy loading.

  9. Crawler and Social Preview Support

    main

    unlazy is designed to work with crawlers and social scrapers (like Googlebot, Slackbot, Twitterbot, and facebookexternalhit).

    Because many scrapers do not execute JavaScript or scroll the page, unlazy detects bot user agents and executes an immediate-swap path. This ensures that crawlers see the real src attribute in the rendered HTML rather than just the data-src placeholder.

  10. Understand the Dev-Mode LCP Warning

    main

    During development, unlazy uses a PerformanceObserver to monitor largest-contentful-paint entries. If the detected LCP element is an <img> that is still configured for lazy loading (i.e., it has loading="lazy" or is still using data-src/data-srcset), unlazy will log a warning to the console:

    [unlazy] LCP element is configured for lazy loading. Set loading="eager" to improve Largest Contentful Paint.

    To remove these warnings from production builds, use the __UNLAZY_LOGGING__ build flag.