@mts-pjsc/image-optimize 1.3.9 → 1.3.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +150 -24
- package/package.json +4 -1
package/README.md
CHANGED
|
@@ -1,41 +1,167 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @mts-pjsc/image-optimize
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/@mts-pjsc/image-optimize)
|
|
4
|
+
[](LICENSE)
|
|
5
|
+
[](CODE_OF_CONDUCT.md)
|
|
4
6
|
|
|
5
|
-
|
|
7
|
+
A React component for automatic image optimization. Works in conjunction with the [Image Optimizer](https://github.com/MobileTeleSystems/image-optimize) microservice.
|
|
6
8
|
|
|
7
|
-
|
|
8
|
-
- Resize images for the user's screen size,
|
|
9
|
-
- Image compressions to reduce traffic,
|
|
10
|
-
- Converting images to modern formats such as webp and avif,
|
|
11
|
-
- Works with dynamic content, compression occurs on the fly,
|
|
12
|
-
- High compression speed, an average picture is processed in just 200 ms,
|
|
13
|
-
- Includes exporter of metrics for Prometheus,
|
|
14
|
-
- Supports basic authorization for multiple domains and endpoints,
|
|
15
|
-
- Supports security restrictions for allowed addresses.
|
|
9
|
+
## Overview
|
|
16
10
|
|
|
17
|
-
|
|
18
|
-
[The optimization microservice](https://github.com/MobileTeleSystems/image-optimize) must be deployed on the server along the path `/optimizer`. React component will use it.
|
|
11
|
+
This library provides a drop-in replacement for the standard `<img>` element that automatically optimizes images based on the user's device, screen size, and browser capabilities. The component communicates with a backend optimization service to deliver the most efficient image format and size.
|
|
19
12
|
|
|
20
|
-
|
|
13
|
+
## Features
|
|
21
14
|
|
|
22
|
-
|
|
15
|
+
- **Responsive resizing** — automatically selects optimal image dimensions based on container size and device pixel ratio
|
|
16
|
+
- **Modern format support** — converts images to WebP or AVIF when supported by the browser
|
|
17
|
+
- **Quality control** — configurable compression quality for fine-tuned optimization
|
|
18
|
+
- **Lazy evaluation** — determines optimal size after component mount for accurate container measurement
|
|
19
|
+
- **Resize handling** — recalculates optimal image size on window resize with debouncing
|
|
20
|
+
- **Next.js compatible** — includes "use client" directive for React Server Components support
|
|
21
|
+
- **Zero configuration** — works out of the box with sensible defaults
|
|
22
|
+
|
|
23
|
+
## Prerequisites
|
|
24
|
+
|
|
25
|
+
The [Image Optimizer](https://github.com/MobileTeleSystems/image-optimize) microservice must be deployed and accessible at the `/optimizer` path on your server.
|
|
26
|
+
|
|
27
|
+
## Installation
|
|
23
28
|
|
|
24
29
|
```bash
|
|
25
|
-
npm
|
|
30
|
+
npm install @mts-pjsc/image-optimize
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Requirements
|
|
34
|
+
|
|
35
|
+
- React 16.0.0 or higher
|
|
36
|
+
|
|
37
|
+
## Basic Usage
|
|
38
|
+
|
|
39
|
+
Replace standard `<img>` elements with the `Image` component:
|
|
40
|
+
|
|
41
|
+
```tsx
|
|
42
|
+
import { Image } from "@mts-pjsc/image-optimize";
|
|
43
|
+
|
|
44
|
+
function App() {
|
|
45
|
+
return (
|
|
46
|
+
<Image
|
|
47
|
+
src="/images/hero-banner.png"
|
|
48
|
+
alt="Hero banner"
|
|
49
|
+
/>
|
|
50
|
+
);
|
|
51
|
+
}
|
|
26
52
|
```
|
|
27
53
|
|
|
28
|
-
|
|
54
|
+
## API Reference
|
|
55
|
+
|
|
56
|
+
### Image Component
|
|
57
|
+
|
|
58
|
+
The `Image` component accepts all standard `<img>` attributes plus the following:
|
|
29
59
|
|
|
30
|
-
|
|
60
|
+
| Prop | Type | Default | Description |
|
|
61
|
+
|------|------|---------|-------------|
|
|
62
|
+
| `src` | `string` | — | Required. Image source URL |
|
|
63
|
+
| `alt` | `string` | — | Required. Alternative text for accessibility |
|
|
64
|
+
| `offset` | `number` | `0` | Adjust the selected size breakpoint (-1 for smaller, +1 for larger) |
|
|
65
|
+
| `quality` | `number` | — | Compression quality (0-100). Uses server default if not specified |
|
|
66
|
+
| `setRef` | `(elem: HTMLImageElement \| null) => void` | — | Callback to receive the underlying img element reference |
|
|
31
67
|
|
|
32
|
-
|
|
68
|
+
### Static Configuration
|
|
33
69
|
|
|
34
|
-
|
|
35
|
-
import {Image} from "@mts-pjsc/image-optimize";
|
|
70
|
+
Configure global behavior through static properties:
|
|
36
71
|
|
|
72
|
+
```tsx
|
|
73
|
+
import { Image } from "@mts-pjsc/image-optimize";
|
|
74
|
+
|
|
75
|
+
// Skip optimization and use original URLs (useful for local development)
|
|
76
|
+
Image.isUseSourceUrl = process.env.NODE_ENV !== "production";
|
|
77
|
+
|
|
78
|
+
// Override image origin for CORS or microfrontend scenarios
|
|
79
|
+
Image.imgOrigin = "https://cdn.example.com";
|
|
80
|
+
|
|
81
|
+
// Custom breakpoints for responsive sizing (default: [160, 320, 640, 1280, 1920])
|
|
82
|
+
Image.controlPoints = [320, 640, 1024, 1440, 2560];
|
|
83
|
+
|
|
84
|
+
// Enable diagnostic logging in development
|
|
85
|
+
Image.isShowDiagnostic = true;
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## Examples
|
|
89
|
+
|
|
90
|
+
### With Quality Control
|
|
91
|
+
|
|
92
|
+
```tsx
|
|
37
93
|
<Image
|
|
38
|
-
|
|
39
|
-
|
|
94
|
+
src="/images/photo.jpg"
|
|
95
|
+
alt="High quality photo"
|
|
96
|
+
quality={85}
|
|
40
97
|
/>
|
|
41
98
|
```
|
|
99
|
+
|
|
100
|
+
### With Size Offset
|
|
101
|
+
|
|
102
|
+
```tsx
|
|
103
|
+
// Request a larger size than calculated (useful for hero images)
|
|
104
|
+
<Image
|
|
105
|
+
src="/images/banner.png"
|
|
106
|
+
alt="Banner"
|
|
107
|
+
offset={1}
|
|
108
|
+
/>
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### With Ref Callback
|
|
112
|
+
|
|
113
|
+
```tsx
|
|
114
|
+
<Image
|
|
115
|
+
src="/images/chart.png"
|
|
116
|
+
alt="Chart"
|
|
117
|
+
setRef={(elem) => {
|
|
118
|
+
if (elem) {
|
|
119
|
+
elem.decode().then(() => console.log("Image decoded"));
|
|
120
|
+
}
|
|
121
|
+
}}
|
|
122
|
+
/>
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### Development Configuration
|
|
126
|
+
|
|
127
|
+
```tsx
|
|
128
|
+
// In your app initialization
|
|
129
|
+
import { Image } from "@mts-pjsc/image-optimize";
|
|
130
|
+
|
|
131
|
+
if (process.env.NODE_ENV !== "production") {
|
|
132
|
+
Image.isUseSourceUrl = true;
|
|
133
|
+
Image.isShowDiagnostic = true;
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## How It Works
|
|
138
|
+
|
|
139
|
+
1. The component mounts and measures the container width
|
|
140
|
+
2. Based on container size and device pixel ratio, it selects an optimal size from the control points
|
|
141
|
+
3. Browser capabilities are detected (AVIF > WebP > original format)
|
|
142
|
+
4. A request URL is constructed: `/optimizer/optimize?src=...&size=...&format=...`
|
|
143
|
+
5. The optimized image is loaded and displayed
|
|
144
|
+
6. On window resize, the process repeats with debouncing to prevent excessive requests
|
|
145
|
+
|
|
146
|
+
## Helper Functions
|
|
147
|
+
|
|
148
|
+
The library also exports utility functions for format detection:
|
|
149
|
+
|
|
150
|
+
```tsx
|
|
151
|
+
import { checkWebpFeature, checkAvifFeature } from "@mts-pjsc/image-optimize";
|
|
152
|
+
|
|
153
|
+
const supportsWebP = await checkWebpFeature();
|
|
154
|
+
const supportsAvif = await checkAvifFeature();
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## License
|
|
158
|
+
|
|
159
|
+
Apache-2.0
|
|
160
|
+
|
|
161
|
+
## Contributing
|
|
162
|
+
|
|
163
|
+
See [CONTRIBUTING.md](./CONTRIBUTING.md) for development guidelines.
|
|
164
|
+
|
|
165
|
+
## Related Projects
|
|
166
|
+
|
|
167
|
+
- [Image Optimizer](https://github.com/MobileTeleSystems/image-optimize) — Backend microservice for image optimization
|
package/package.json
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mts-pjsc/image-optimize",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.11",
|
|
4
4
|
"description": "React component for image optimizer",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.js",
|
|
7
7
|
"esnext": "dist/index.js",
|
|
8
8
|
"type": "module",
|
|
9
|
+
"files": [
|
|
10
|
+
"dist"
|
|
11
|
+
],
|
|
9
12
|
"scripts": {
|
|
10
13
|
"eslint": "eslint --fix -c eslint.config.js --ext .tsx,.ts,.jsx,.js ./src/",
|
|
11
14
|
"test": "echo \"Error: no test specified. For test need deployed microservice part\" && exit 1",
|