From a7f472b59cdfba26e533e98edce5c06bd7a040c6 Mon Sep 17 00:00:00 2001 From: Filip Drozd Date: Fri, 24 Mar 2023 10:26:35 +0100 Subject: [PATCH 1/2] fix: prepend hostname to image urls only when it's not preset --- packages/api-client/src/api/serializers/product.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/api-client/src/api/serializers/product.ts b/packages/api-client/src/api/serializers/product.ts index 95f63c63..d2290b0c 100644 --- a/packages/api-client/src/api/serializers/product.ts +++ b/packages/api-client/src/api/serializers/product.ts @@ -237,7 +237,7 @@ const addHostToImage = (image, config: ApiConfig) => ({ styles: image.attributes?.styles ? image.attributes.styles.map((style) => ({ width: style.width, height: style.height, - url: (config.assetsUrl || config.backendUrl) + style.url + url: `${style.url.includes('http') ? '' : (config.assetsUrl || config.backendUrl)}${style.url}` })) : [] } }); From 2bb93633056d99293b9a8b5ee90d6a0842990147 Mon Sep 17 00:00:00 2001 From: Filip Drozd Date: Mon, 27 Mar 2023 15:42:14 +0200 Subject: [PATCH 2/2] refactor: extract host to a variable --- .../api-client/src/api/serializers/product.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/packages/api-client/src/api/serializers/product.ts b/packages/api-client/src/api/serializers/product.ts index d2290b0c..1d1f6970 100644 --- a/packages/api-client/src/api/serializers/product.ts +++ b/packages/api-client/src/api/serializers/product.ts @@ -234,11 +234,17 @@ const addHostToImage = (image, config: ApiConfig) => ({ ...image, attributes: { ...image.attributes, - styles: image.attributes?.styles ? image.attributes.styles.map((style) => ({ - width: style.width, - height: style.height, - url: `${style.url.includes('http') ? '' : (config.assetsUrl || config.backendUrl)}${style.url}` - })) : [] + styles: image.attributes?.styles ? image.attributes.styles.map((style) => { + const host = style.url.startsWith('http://') || style.url.startsWith('https://') + ? '' + : (config.assetsUrl || config.backendUrl); + + return { + width: style.width, + height: style.height, + url: `${host}${style.url}` + }; + }) : [] } });