You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
unlazy includes [BlurHash](/placeholders/blurhash) and [ThumbHash](/placeholders/thumbhash) decoding algorithms (from `fast-blurhash` and `thumbhash` packages). If your project doesn't use hash-based placeholders, you can exclude these dependencies entirely:
29
+
unlazy includes [BlurHash](/placeholders/hash-based#blurhash) and [ThumbHash](/placeholders/hash-based#thumbhash) decoding algorithms (from `fast-blurhash` and `thumbhash` packages). If your project doesn't use hash-based placeholders, you can exclude these dependencies entirely:
30
30
31
31
-`__UNLAZY_HASH_DECODING__`: Set to `false` to tree-shake hash decoding code. Default is `true`.
32
32
@@ -41,12 +41,12 @@ This flag only affects the main entry point. If you directly import `unlazy/blur
unlazy will help you locate missing `data-src` or `data-srcset` attributes in your project by logging a warning in the browser console. An example warning message looks like this:
44
+
unlazy helps you locate missing `data-src` or `data-srcset` attributes by logging warnings in the browser console. An example warning message looks like this:
45
45
46
46
```
47
47
[unlazy] Missing `data-src` or `data-srcset` attribute: <img>
48
48
```
49
49
50
-
If you want to disable these warnings, you can use the following build flag:
50
+
To disable these warnings, use the following build flag:
51
51
52
-
-`__UNLAZY_LOGGING__`: This flag is set to `true` by default.
52
+
-`__UNLAZY_LOGGING__`: Set to `false`to disable warnings. Default is `true`.
This is a collection of recipes for common use cases, since unlazy is a flexible library that can be used in many ways. If you have a use case that is not covered here, please [open an issue](https://github.com/johannschopplich/unlazy/issues).
3
+
This section covers advanced configuration options and optimization techniques for unlazy. These topics help you fine-tune performance and behavior for production applications.
4
4
5
-
-[Aspect Ratio for Blurry Placeholders](/advanced/aspect-ratio)
6
-
-[Build Flags to Tree-Shake Unused Code](/advanced/build-flags)
5
+
## Topics
6
+
7
+
-**[Aspect Ratio](/advanced/aspect-ratio)** – Prevent CLS with aspect ratio settings for blurry placeholders
8
+
-**[Build Flags](/advanced/build-flags)** – Tree-shake unused features to reduce bundle size
9
+
10
+
If you have a use case not covered here, please [open an issue](https://github.com/johannschopplich/unlazy/issues).
unlazy provides a number of methods to help you with lazy loading images. The following methods are available:
3
+
unlazy provides a number of methods to help you with lazy loading images:
4
4
5
5
-[`lazyLoad`](/api/lazy-load) – Lazy load images with automatic placeholder generation
6
6
-[`autoSizes`](/api/auto-sizes) – Automatically calculate the `sizes` attribute
7
7
-[`loadImage`](/api/load-image) – Manually load an image before it enters the viewport
8
8
-[`createPlaceholderFromHash`](/api/create-placeholder-from-hash) – Generate PNG data URI from BlurHash or ThumbHash
9
-
10
-
## Server-Side Rendering
11
-
12
-
unlazy supports server-side rendering for BlurHash and ThumbHash strings. This means that you can generate the placeholder images for the `src` attribute during SSR and avoid the [Cumulative Layout Shift](https://web.dev/cls/) (CLS) caused by the images loading after the page has been rendered.
13
-
14
-
Both the `unlazy/blurhash` and `unlazy/thumbhash` exports provide a `createPngDataUri` function that can be used to generate a PNG data URI for a BlurHash or ThumbHash string, respectively. This function can be used to generate the `src` attribute for the `<img>` element.
15
-
16
-
-[`createPngDataUri`](/api/blurhash-create-png-data-uri) – Generate a PNG data URI for a BlurHash string
17
-
-[`createPngDataUri`](/api/thumbhash-create-png-data-uri) – Generate a PNG data URI for a ThumbHash string
9
+
- BlurHash [`createPngDataUri`](/api/blurhash-create-png-data-uri) – Generate PNG data URI for SSR
10
+
- ThumbHash [`createPngDataUri`](/api/thumbhash-create-png-data-uri) – Generate PNG data URI for SSR
Copy file name to clipboardExpand all lines: docs/guide/index.md
+1-3Lines changed: 1 addition & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,6 +30,4 @@ unlazy ensures that search engines can index the full-quality images by detectin
30
30
31
31
Head over to the [installation guide](/guide/installation) to learn how to install unlazy in your project. After that, you can check out the [usage guide](/guide/usage) and may take a look at the [API reference](/api/).
32
32
33
-
If your project is using [BlurHash](https://blurha.sh), you can check out the [BlurHash guide](/placeholders/blurhash) to learn how to use it with unlazy.
34
-
35
-
The shiny new [ThumbHash](https://github.com/evanw/thumbhash) by Evan Wallace is also supported. You can check out the [ThumbHash guide](/placeholders/thumbhash) to learn how to use it with unlazy.
33
+
If you want to use hash-based placeholders like [BlurHash](https://blurha.sh) or [ThumbHash](https://github.com/evanw/thumbhash), check out the [hash-based placeholders guide](/placeholders/hash-based).
Copy file name to clipboardExpand all lines: docs/guide/installation.md
+10-14Lines changed: 10 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -31,20 +31,16 @@ For more use cases, head over to the [Usage](/guide/usage) guide.
31
31
32
32
## Without a Build Step
33
33
34
-
unlazy can be used without a build step. Useful for prototyping or when you do not want to add a build step to your project. Simply load it from a CDN:
34
+
unlazy can be used without a build step from a CDN. Useful for prototyping or projects without bundlers.
35
35
36
-
- Global build: <CdnLinkname="unlazy.iife.js" />
37
-
- Exposes `UnLazy` global property, supports auto initializing
-**With Hashing Global**: <CdnLinkname="unlazy.with-hashing.iife.js" /> - Includes BlurHash/ThumbHash decoders
40
+
-**With Hashing ESM**: <CdnLinkname="unlazy.with-hashing.js" /> - ESM with hash decoding
40
41
41
42
::: info
42
-
To keep the bundle size small, the standard global build excludes BlurHash and ThumbHash decoding algorithms. Use the `with-hashing` variant if you need client-side hash decoding:
43
-
44
-
- Global build with hashing: <CdnLinkname="unlazy.with-hashing.iife.js" />
45
-
- ESM build with hashing: <CdnLinkname="unlazy.with-hashing.js" />
46
-
47
-
If you decode hashes on the server (e.g., during SSR), use the standard bundle instead.
43
+
Use standard builds if you decode hashes on the server (SSR). Use `with-hashing` builds only if you need client-side hash decoding.
48
44
:::
49
45
50
46
### Auto-Initialization
@@ -59,12 +55,12 @@ When using the global build, you can use the `init` attribute to automatically i
59
55
- The `init` attribute tells the library to automatically initialize and watch all elements that have a `loading="lazy"` attribute.
60
56
61
57
::: info
62
-
The short CDN URLs are meant for prototyping. For production usage, use a fully resolved CDN URL to avoid resolving and redirect cost:
58
+
The short CDN URLs are meant for prototyping. For production usage, use a fully resolved CDN URL to avoid resolving and redirect costs, for example: `https://unpkg.com/[email protected]/dist/unlazy.iife.js`.
63
59
:::
64
60
65
61
### Manual Initialization
66
62
67
-
If you do not want the auto-initialize the library, remove the `init` attribute and move the scripts to end of the `<body>` tag:
63
+
If you do not want to auto-initialize the library, remove the `init` attribute and move the scripts to end of the `<body>` tag:
68
64
69
65
```html
70
66
<scriptsrc="https://unpkg.com/unlazy"></script>
@@ -74,7 +70,7 @@ If you do not want the auto-initialize the library, remove the `init` attribute
74
70
```
75
71
76
72
::: info
77
-
The short CDN URLs are meant for prototyping. For production usage, use a fully resolved CDN URL to avoid resolving and redirect cost:
73
+
The short CDN URLs are meant for prototyping. For production usage, use a fully resolved CDN URL to avoid resolving and redirect costs, for example: `https://unpkg.com/[email protected]/dist/unlazy.iife.js`.
When implementing lazy loading with blurry placeholders, it is important to consider the impact it may have on SEO. Search engine bots and crawlers are responsible for indexing and ranking your website's content, and they might not always behave like human visitors when browsing the site. unlazy ensures that your website's images are indexed correctly and that their quality is not negatively affected by the use of blurry placeholders.
3
+
When implementing lazy loading with blurry placeholders, consider the SEO impact. Search engine bots and crawlers may not behave like human visitors. unlazy ensures images are indexed correctly without compromising quality due to blurry placeholders.
4
4
5
5
## Detects Bots and Crawlers
6
6
7
-
unlazy includes an exported function called `isCrawler` that detects if the visitor is a bot or a crawler. This ensures that the full-quality image is loaded and indexed by search engines.
7
+
unlazy includes an exported `isCrawler`function that detects bots and crawlers, ensuring full-quality images are loaded and indexed by search engines.
8
8
9
9
The detection mechanism works by:
10
10
@@ -16,6 +16,6 @@ The library trusts that bots and crawlers can evaluate the `srcset` attribute an
16
16
17
17
## Use `src`, `data-srcset` and `sizes`, or `data-src` Attributes
18
18
19
-
When using blurry placeholders, it is important to set the `src` attribute with the blurry placeholder image and use the `data-srcset` (or `data-src`) attribute for the high-quality image. This ensures that the initial rendering of the page will display the blurry placeholders, while the full-quality images will be loaded later as they enter the viewport.
19
+
Set the `src` attribute with the blurry placeholder and use `data-srcset` (or `data-src`) for the high-quality image. This displays placeholders during initial render while loading full-quality images as they enter the viewport.
20
20
21
-
Additionally, using the `sizes` attribute (or [`data-sizes="auto"`](/guide/usage#auto-calculation-of-the-sizes-attribute) for automatic calculation) helps provide the search engine with information about the intended display size of the image. This enables search engines to select the most appropriate image source from the `srcset` attribute when indexing your website's content.
21
+
Use the `sizes` attribute (or [`data-sizes="auto"`](/guide/usage#auto-calculation-of-the-sizes-attribute) for automatic calculation) to provide search engines with the intended display size. This enables search engines to select the appropriate image source from the `srcset` attribute when indexing.
Copy file name to clipboardExpand all lines: docs/guide/usage.md
+6-6Lines changed: 6 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,9 +2,9 @@
2
2
3
3
1. Add the `loading="lazy"` attribute to a `<img>` element that you want to lazily load. You can also use the `<picture>` element to lazily load images in different formats.
4
4
5
-
2. Use the `data-src` or `data-srcset`attribute to specify the high-quality image. These data attributes prevent browsers from loading images before they enter the viewport and are swapped to standard attributes by unlazy when loading occurs.
5
+
2. Use `data-src` or `data-srcset`attributes to specify the high-quality image. These prevent browsers from loading images before they enter the viewport and are swapped to standard attributes when loading occurs.
6
6
7
-
3.If you have a pre-generated blurry placeholder image, use the `src` attribute to specify it. Otherwise, you can use a [BlurHash](/placeholders/blurhash)or [ThumbHash](/placeholders/thumbhash) to generate a placeholder image on the fly.
7
+
3.Set the `src` attribute with a pre-generated blurry placeholder, or use [hash-based placeholders](/placeholders/hash-based) (BlurHash or ThumbHash) to generate placeholders on the fly.
8
8
9
9
::: code-group
10
10
```html [Image tag]
@@ -51,7 +51,7 @@ const cleanup = lazyLoad()
51
51
// Useful when unmounting components or removing images dynamically
52
52
```
53
53
54
-
The `lazyLoad` function returns a cleanup function that removes all event listeners and ResizeObservers. Call this function when unmounting components or when images are no longer needed to prevent memory leaks.
54
+
The `lazyLoad` function returns a cleanup function that removes event listeners and ResizeObservers. Call this when unmounting components or removing images to prevent memory leaks.
55
55
56
56
## Auto Calculation of the `sizes` Attribute
57
57
@@ -68,9 +68,9 @@ The automatic sizes calculation uses the display width of the image.
68
68
>
69
69
```
70
70
71
-
When calling [`lazyLoad`](/api/lazy-load), the library will automatically calculate the `sizes` attribute for all images with `data-sizes="auto"`.
71
+
When calling [`lazyLoad`](/api/lazy-load), the library automatically calculates the `sizes` attribute for all images with `data-sizes="auto"`.
72
72
73
-
Alternatively, you can use the [`autoSizes`](/api/auto-sizes) function to calculate the `sizes` attribute for all images with `data-sizes="auto"`, without lazy loading the images.
73
+
Alternatively, use the [`autoSizes`](/api/auto-sizes) function to calculate the `sizes` attribute without lazy loading.
74
74
75
75
To do so, import the [`autoSizes`](/api/auto-sizes) function from the library and call it:
76
76
@@ -113,5 +113,5 @@ loadImage(coolImage)
113
113
```
114
114
115
115
::: tip
116
-
Keep in mind that manually loading images might negatively affect the perceived performance, as it will force the full-quality image to load immediately, even if it is not visible on the viewport.
116
+
Manually loading images may negatively affect perceived performance by forcing immediate load, even when not visible in the viewport.
Copy file name to clipboardExpand all lines: docs/integrations/index.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,9 +2,9 @@
2
2
3
3
unlazy supports lazy loading images in your framework of choice. Each package provides a `UnLazyImage` component as a drop-in replacement for the native `<img>` element.
4
4
5
-
## Generic How-To
5
+
## Creating Custom Integrations
6
6
7
-
To create a component for the framework of your choice, keep the following in mind:
7
+
To create a component for your framework, keep the following in mind:
8
8
9
9
1. The component should wrap a native `<img>` element with the `loading="lazy"` attribute set.
10
10
2. The component should support at least a `src` or `srcset` attribute.
@@ -60,7 +60,7 @@ The following integrations are available as of now:
60
60
### Meta-Frameworks
61
61
62
62
::: tip
63
-
These include SSR-decoding of [BlurHash](/placeholders/blurhash)and [ThumbHash](/placeholders/thumbhash) strings.
63
+
These include SSR-decoding of [hash-based placeholders](/placeholders/hash-based) (BlurHash and ThumbHash).
0 commit comments