Skip to content

Commit 4a1a082

Browse files
committed
Graphql transforms for thumbnails
1 parent 83e1eab commit 4a1a082

6 files changed

Lines changed: 14 additions & 59 deletions

File tree

sample/@types/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export interface ShopifyProduct {
2626
id: string;
2727
altText: string;
2828
url: string;
29+
thumbnailUrl: string;
2930
}>;
3031
variants: Edges<ProductVariant>;
3132
}
@@ -41,6 +42,7 @@ export interface CartItem {
4142
width: number;
4243
height: number;
4344
url: string;
45+
thumbnailUrl: string;
4446
altText: string;
4547
};
4648
}

sample/src/hooks/useShopify.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const productFragment = gql`
4646
width
4747
height
4848
url
49+
thumbnailUrl: url(transform: {maxWidth: 80, maxHeight: 80})
4950
}
5051
}
5152
@@ -95,6 +96,7 @@ const PRODUCTS_QUERY = gql`
9596
width
9697
height
9798
url
99+
thumbnailUrl: url(transform: {maxWidth: 150, maxHeight: 150})
98100
}
99101
}
100102
}

sample/src/screens/CartScreen.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,15 @@ function CartItem({
203203
...styles.productItem,
204204
...(loading ? styles.productItemLoading : {}),
205205
}}>
206-
{item.merchandise.image?.url && (
206+
{item.merchandise.image?.thumbnailUrl && (
207207
<Image
208208
resizeMethod="resize"
209209
resizeMode="cover"
210210
style={styles.productImage}
211211
alt={item.merchandise.image?.altText}
212-
source={{uri: item.merchandise.image?.url}}
212+
source={{
213+
uri: item.merchandise.image?.thumbnailUrl,
214+
}}
213215
/>
214216
)}
215217
<View style={styles.productText}>

sample/src/screens/CatalogScreen.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ function Product({
150150
resizeMode="cover"
151151
style={styles.productImage}
152152
alt={image?.altText}
153-
source={{uri: image?.url}}
153+
source={{
154+
uri: image.thumbnailUrl,
155+
}}
154156
/>
155157
)}
156158
<View style={styles.productText}>

sample/src/screens/ProductDetailsScreen.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ function ProductDetails({
9797
resizeMode="cover"
9898
style={styles.productImage}
9999
alt={image?.altText}
100-
source={{uri: image?.url}}
100+
source={{
101+
uri: image.thumbnailUrl,
102+
}}
101103
/>
102104
)}
103105
<View style={styles.productText}>

sample/src/utils.ts

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -73,61 +73,6 @@ export function currency(amount?: string, currency?: string): string {
7373
}
7474
}
7575

76-
/**
77-
* Optimizes a Shopify image URL by requesting a specific size
78-
* This significantly reduces bandwidth and improves loading times
79-
*
80-
* @param url - Original Shopify CDN image URL
81-
* @param size - Desired image dimensions
82-
* @param scale - Pixel density multiplier (default: 2 for retina displays)
83-
* @returns Optimized image URL with size parameters
84-
*
85-
* @example For product thumbnails
86-
* getOptimizedImageUrl(imageUrl, {width: 150, height: 150})
87-
*
88-
* @example For product detail images
89-
* getOptimizedImageUrl(imageUrl, {width: 400, height: 400})
90-
*
91-
* @example For cart item thumbnails
92-
* getOptimizedImageUrl(imageUrl, {width: 80, height: 80})
93-
*/
94-
export function getOptimizedImageUrl(
95-
url: string | undefined,
96-
size: {width: number; height: number},
97-
scale: number = 2,
98-
): string | undefined {
99-
if (!url) {
100-
return undefined;
101-
}
102-
103-
try {
104-
// Calculate actual dimensions for device pixel density
105-
const width = size.width * scale;
106-
const height = size.height * scale;
107-
108-
// Shopify CDN image transformation pattern:
109-
// Original: https://cdn.shopify.com/s/files/1/0001/0002/products/image.jpg
110-
// Resized: https://cdn.shopify.com/s/files/1/0001/0002/products/image_600x600.jpg
111-
112-
if (url.includes('cdn.shopify.com')) {
113-
// Remove any existing size parameters
114-
const cleanUrl = url.replace(/_\d+x\d+(\.\w+)/, '$1');
115-
116-
// Insert new size parameters before file extension
117-
const lastDot = cleanUrl.lastIndexOf('.');
118-
if (lastDot > -1) {
119-
return `${cleanUrl.slice(0, lastDot)}_${width}x${height}${cleanUrl.slice(lastDot)}`;
120-
}
121-
}
122-
123-
// Return original URL if not a Shopify CDN image
124-
return url;
125-
} catch {
126-
// Return original URL if transformation fails
127-
return url;
128-
}
129-
}
130-
13176
export function debugLog(message: string, data?: any) {
13277
if (__DEV__) {
13378
console.log(message, data || '');

0 commit comments

Comments
 (0)