@@ -68,7 +68,7 @@ export class HTMLTemplate {
68
68
this . ctx = ctx ;
69
69
this . product = product ;
70
70
this . variants = variants ;
71
- this . image = findProductImage ( product , variants ) ;
71
+ this . image = this . constructImage ( findProductImage ( product , variants ) ) ;
72
72
}
73
73
74
74
/**
@@ -181,15 +181,18 @@ ${HTMLTemplate.indent(this.renderJSONLD(), 2)}
181
181
return /* html */ `\
182
182
<div class="product-images">
183
183
<div>
184
- ${ images . map ( ( img ) => /* html */ `\
184
+ ${ images . map ( this . constructImage . bind ( this ) )
185
+ . filter ( ( img ) => Boolean ( img ) )
186
+ . map ( ( img ) => /* html */ `\
185
187
<div>
186
188
<picture>
187
189
<source type="image/webp" srcset="${ img . url } " alt="" media="(min-width: 600px)">
188
190
<source type="image/webp" srcset="${ img . url } ">
189
191
<source type="image/png" srcset="${ img . url } " media="(min-width: 600px)">
190
192
<img loading="lazy" alt="${ img . label } " src="${ img . url } ">
191
193
</picture>
192
- </div>` ) . join ( '\n' ) }
194
+ </div>` )
195
+ . join ( '\n' ) }
193
196
</div>
194
197
</div>` ;
195
198
}
@@ -249,19 +252,46 @@ ${HTMLTemplate.indent(this.renderProductItems(opt.items), 2)}`).join('\n')}
249
252
</div>` : '' ;
250
253
}
251
254
255
+ /**
256
+ * @param {Image } image
257
+ * @returns {Image | null }
258
+ */
259
+ constructImage ( image ) {
260
+ if ( ! image || ! image . url ) {
261
+ return null ;
262
+ }
263
+
264
+ if ( ! this . ctx . config . imageParams ) {
265
+ return image ;
266
+ }
267
+
268
+ // append image params
269
+ const { url : purl , label } = image ;
270
+ const url = new URL ( purl ) ;
271
+ const params = new URLSearchParams ( this . ctx . config . imageParams ) ;
272
+ url . search = params . toString ( ) ;
273
+ return {
274
+ url : url . toString ( ) ,
275
+ label,
276
+ } ;
277
+ }
278
+
252
279
/**
253
280
* Create the variant images
254
281
* @param {Image[] } images
255
282
* @returns {string }
256
283
*/
257
284
renderVariantImages ( images ) {
258
- return images . map ( ( img ) => /* html */ `\
285
+ return images . map ( this . constructImage . bind ( this ) )
286
+ . filter ( ( img ) => Boolean ( img ) )
287
+ . map ( ( img ) => /* html */ `\
259
288
<picture>
260
289
<source type="image/webp" srcset="${ img . url } " alt="" media="(min-width: 600px)">
261
290
<source type="image/webp" srcset="${ img . url } ">
262
291
<source type="image/png" srcset="${ img . url } " media="(min-width: 600px)">
263
292
<img loading="lazy" alt="${ img . label } " src="${ img . url } ">
264
- </picture>` ) . join ( '\n' ) ;
293
+ </picture>` )
294
+ . join ( '\n' ) ;
265
295
}
266
296
267
297
/**
0 commit comments