-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebp-image-changes.patch
More file actions
234 lines (228 loc) · 7.77 KB
/
webp-image-changes.patch
File metadata and controls
234 lines (228 loc) · 7.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
diff --git a/components/FeatureCard.jsx b/components/FeatureCard.jsx
index 974d7d2..3e6a4fd 100644
--- a/components/FeatureCard.jsx
+++ b/components/FeatureCard.jsx
@@ -12,16 +12,33 @@ export default function FeatureCard({
// Handle image path with correct basePath for both dev and production
const imagePath = image.startsWith('/') ? image : `/${image}`;
-
- // Create GitHub CDN URL for the image
+
+ // Get the image name without extension for WebP conversion
+ const imagePathInfo = imagePath.match(/(.+)\.([^.]+)$/);
+ if (!imagePathInfo) {
+ console.error(`Invalid image path: ${imagePath}`);
+ return null;
+ }
+
+ const imagePathWithoutExt = imagePathInfo[1];
+ const imageExt = imagePathInfo[2];
+
+ // Create WebP path
+ const webpPath = `${imagePathWithoutExt.replace('/images/', '/images/webp/')}.webp`;
+
+ // Create GitHub CDN URLs for WebP and original images
const getGithubCdnUrl = (path) => {
// Remove leading slash if present
const cleanPath = path.startsWith('/') ? path.slice(1) : path;
return `https://raw.githubusercontent.com/DspreadOrg/docs/main/public/${cleanPath}`;
};
-
- // Use local path in development, CDN in production
- const imageSrc = isDev
+
+ // Use local paths in development, CDN in production
+ const webpImageSrc = isDev
+ ? `${basePath}${webpPath}`
+ : getGithubCdnUrl(webpPath);
+
+ const originalImageSrc = isDev
? `${basePath}${imagePath}`
: getGithubCdnUrl(imagePath);
@@ -38,17 +55,21 @@ export default function FeatureCard({
<div className="feature-card">
<div className="feature-card-content">
<a href={fullPath} className="feature-card-image-link">
- <img
- src={imageSrc}
- alt={linkText || 'Feature image'}
- className="feature-card-image"
- loading="lazy"
- onError={(e) => {
- console.error(`Failed to load image: ${imageSrc}`);
- e.target.onerror = null;
- e.target.src = fallbackImagePath;
- }}
- />
+ <picture>
+ <source srcSet={webpImageSrc} type="image/webp" />
+ <source srcSet={originalImageSrc} type={`image/${imageExt}`} />
+ <img
+ src={originalImageSrc}
+ alt={linkText || 'Feature image'}
+ className="feature-card-image"
+ loading="lazy"
+ onError={(e) => {
+ console.error(`Failed to load image: ${e.target.src}`);
+ e.target.onerror = null;
+ e.target.src = fallbackImagePath;
+ }}
+ />
+ </picture>
</a>
</div>
<a href={fullPath} className="feature-card-link-text">
diff --git a/components/ResponsiveImage.jsx b/components/ResponsiveImage.jsx
index df1c815..2f01892 100644
--- a/components/ResponsiveImage.jsx
+++ b/components/ResponsiveImage.jsx
@@ -16,16 +16,33 @@ export default function ResponsiveImage({
// Handle image path with correct basePath for both dev and production
const imagePath = src.startsWith('/') ? src : `/${src}`;
-
- // Create GitHub CDN URL for the image
+
+ // Get the image name without extension for WebP conversion
+ const imagePathInfo = imagePath.match(/(.+)\.([^.]+)$/);
+ if (!imagePathInfo) {
+ console.error(`Invalid image path: ${imagePath}`);
+ return null;
+ }
+
+ const imagePathWithoutExt = imagePathInfo[1];
+ const imageExt = imagePathInfo[2];
+
+ // Create WebP path
+ const webpPath = `${imagePathWithoutExt.replace('/images/', '/images/webp/')}.webp`;
+
+ // Create GitHub CDN URLs for WebP and original images
const getGithubCdnUrl = (path) => {
// Remove leading slash if present
const cleanPath = path.startsWith('/') ? path.slice(1) : path;
return `https://raw.githubusercontent.com/DspreadOrg/docs/main/public/${cleanPath}`;
};
-
- // Use local path in development, CDN in production
- const imageSrc = isDev
+
+ // Use local paths in development, CDN in production
+ const webpImageSrc = isDev
+ ? `${basePath}${webpPath}`
+ : getGithubCdnUrl(webpPath);
+
+ const originalImageSrc = isDev
? `${basePath}${imagePath}`
: getGithubCdnUrl(imagePath);
@@ -35,20 +52,24 @@ export default function ResponsiveImage({
: getGithubCdnUrl('dspread-logo.png');
return (
- <img
- src={imageSrc}
- alt={alt || 'Image'}
- className={className}
- style={style}
- width={width}
- height={height}
- loading="lazy"
- onError={(e) => {
- console.error(`Failed to load image: ${imageSrc}`);
- e.target.onerror = null;
- e.target.src = fallbackImagePath;
- }}
- {...props}
- />
+ <picture>
+ <source srcSet={webpImageSrc} type="image/webp" />
+ <source srcSet={originalImageSrc} type={`image/${imageExt}`} />
+ <img
+ src={originalImageSrc}
+ alt={alt || 'Image'}
+ className={className}
+ style={style}
+ width={width}
+ height={height}
+ loading="lazy"
+ onError={(e) => {
+ console.error(`Failed to load image: ${e.target.src}`);
+ e.target.onerror = null;
+ e.target.src = fallbackImagePath;
+ }}
+ {...props}
+ />
+ </picture>
);
}
diff --git a/utils/imageUtils.js b/utils/imageUtils.js
index e95fad6..1ffb0f2 100644
--- a/utils/imageUtils.js
+++ b/utils/imageUtils.js
@@ -5,27 +5,64 @@
/**
* Creates a GitHub CDN URL for an image
* @param {string} path - The path to the image (e.g., '/images/example.png')
+ * @param {boolean} useWebP - Whether to use WebP format if available
* @returns {string} The GitHub CDN URL
*/
-export function getGithubCdnUrl(path) {
+export function getGithubCdnUrl(path, useWebP = false) {
// Remove leading slash if present
const cleanPath = path.startsWith('/') ? path.slice(1) : path;
+
+ if (useWebP) {
+ // Convert path to WebP format (e.g., images/file.png -> images/webp/file.webp)
+ const pathInfo = cleanPath.match(/(.+)\.([^.]+)$/);
+ if (pathInfo) {
+ const pathWithoutExt = pathInfo[1];
+ return `https://raw.githubusercontent.com/DspreadOrg/docs/main/public/${pathWithoutExt.replace('images/', 'images/webp/')}.webp`;
+ }
+ }
+
return `https://raw.githubusercontent.com/DspreadOrg/docs/main/public/${cleanPath}`;
}
/**
- * Creates an image source with proper path handling for development and production
+ * Creates image sources with WebP and fallback support
* @param {string} path - The path to the image (e.g., '/images/example.png')
* @param {boolean} isDev - Whether the app is running in development mode
* @param {string} basePath - The base path for the app
- * @returns {string} The image source URL
+ * @returns {Object} An object with webpSrc, originalSrc, and imageType
*/
-export function getImageSrc(path, isDev, basePath = '') {
+export function getImageSources(path, isDev, basePath = '') {
// Handle image path with correct basePath
const imagePath = path.startsWith('/') ? path : `/${path}`;
-
+
+ // Get the image name without extension
+ const imagePathInfo = imagePath.match(/(.+)\.([^.]+)$/);
+ if (!imagePathInfo) {
+ return {
+ webpSrc: null,
+ originalSrc: null,
+ imageType: null
+ };
+ }
+
+ const imagePathWithoutExt = imagePathInfo[1];
+ const imageExt = imagePathInfo[2];
+
+ // Create WebP path
+ const webpPath = `${imagePathWithoutExt.replace('/images/', '/images/webp/')}.webp`;
+
// Use local paths in development, CDN in production
- return isDev
+ const webpImageSrc = isDev
+ ? `${basePath}${webpPath}`
+ : getGithubCdnUrl(imagePath, true);
+
+ const originalImageSrc = isDev
? `${basePath}${imagePath}`
: getGithubCdnUrl(imagePath);
+
+ return {
+ webpSrc: webpImageSrc,
+ originalSrc: originalImageSrc,
+ imageType: imageExt
+ };
}