Skip to content

Commit c78017d

Browse files
committed
feat: image params in config
1 parent bccad87 commit c78017d

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

src/templates/html/HTMLTemplate.js

+35-5
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export class HTMLTemplate {
6868
this.ctx = ctx;
6969
this.product = product;
7070
this.variants = variants;
71-
this.image = findProductImage(product, variants);
71+
this.image = this.constructImage(findProductImage(product, variants));
7272
}
7373

7474
/**
@@ -181,15 +181,18 @@ ${HTMLTemplate.indent(this.renderJSONLD(), 2)}
181181
return /* html */ `\
182182
<div class="product-images">
183183
<div>
184-
${images.map((img) => /* html */ `\
184+
${images.map(this.constructImage.bind(this))
185+
.filter((img) => Boolean(img))
186+
.map((img) => /* html */ `\
185187
<div>
186188
<picture>
187189
<source type="image/webp" srcset="${img.url}" alt="" media="(min-width: 600px)">
188190
<source type="image/webp" srcset="${img.url}">
189191
<source type="image/png" srcset="${img.url}" media="(min-width: 600px)">
190192
<img loading="lazy" alt="${img.label}" src="${img.url}">
191193
</picture>
192-
</div>`).join('\n')}
194+
</div>`)
195+
.join('\n')}
193196
</div>
194197
</div>`;
195198
}
@@ -249,19 +252,46 @@ ${HTMLTemplate.indent(this.renderProductItems(opt.items), 2)}`).join('\n')}
249252
</div>` : '';
250253
}
251254

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+
252279
/**
253280
* Create the variant images
254281
* @param {Image[]} images
255282
* @returns {string}
256283
*/
257284
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 */ `\
259288
<picture>
260289
<source type="image/webp" srcset="${img.url}" alt="" media="(min-width: 600px)">
261290
<source type="image/webp" srcset="${img.url}">
262291
<source type="image/png" srcset="${img.url}" media="(min-width: 600px)">
263292
<img loading="lazy" alt="${img.label}" src="${img.url}">
264-
</picture>`).join('\n');
293+
</picture>`)
294+
.join('\n');
265295
}
266296

267297
/**

src/types.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ declare global {
4747
offerVariantURLTemplate?: string;
4848
attributeOverrides?: AttributeOverrides;
4949
siteOverrides?: Record<string, Record<string, unknown>>;
50+
imageParams?: Record<string, string | boolean | number>;
5051

5152
confMap: ConfigMap;
5253
}

0 commit comments

Comments
 (0)