Skip to content

Commit

Permalink
Simplify image loader implementation by using lossy param directly fr…
Browse files Browse the repository at this point in the history
…om API (#1391)
  • Loading branch information
bookernath authored Sep 23, 2024
1 parent 5a6b2f5 commit 4d64c31
Show file tree
Hide file tree
Showing 15 changed files with 20 additions and 29 deletions.
5 changes: 5 additions & 0 deletions .changeset/strange-beers-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bigcommerce/catalyst-core": patch
---

Get lossy image from API instead of setting param in code
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const SharingLinksFragment = graphql(`
post(entityId: $entityId) {
entityId
thumbnailImage {
url: urlTemplate
url: urlTemplate(lossy: true)
}
seo {
pageTitle
Expand Down
2 changes: 1 addition & 1 deletion core/app/[locale]/(default)/blog/[blogId]/page-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const BlogPageQuery = graphql(
tags
thumbnailImage {
altText
url: urlTemplate
url: urlTemplate(lossy: true)
}
seo {
pageTitle
Expand Down
4 changes: 2 additions & 2 deletions core/app/[locale]/(default)/cart/_components/cart-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const PhysicalItemFragment = graphql(`
brand
sku
image {
url: urlTemplate
url: urlTemplate(lossy: true)
}
entityId
quantity
Expand Down Expand Up @@ -70,7 +70,7 @@ const DigitalItemFragment = graphql(`
brand
sku
image {
url: urlTemplate
url: urlTemplate(lossy: true)
}
entityId
quantity
Expand Down
2 changes: 1 addition & 1 deletion core/app/[locale]/(default)/compare/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const ComparePageQuery = graphql(
}
defaultImage {
altText
url: urlTemplate
url: urlTemplate(lossy: true)
}
reviewSummary {
numberOfReviews
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ export const GalleryFragment = graphql(`
edges {
node {
altText
url: urlTemplate
url: urlTemplate(lossy: true)
isDefault
}
}
}
defaultImage {
altText
url: urlTemplate
url: urlTemplate(lossy: true)
}
}
`);
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const MultipleChoiceFieldFragment = graphql(`
__typename
defaultImage {
altText
url: urlTemplate
url: urlTemplate(lossy: true)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const ProductSchemaFragment = graphql(`
numberOfReviews
}
defaultImage {
url: urlTemplate
url: urlTemplate(lossy: true)
}
prices {
price {
Expand Down
2 changes: 1 addition & 1 deletion core/app/[locale]/(default)/product/[slug]/page-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const ProductPageQuery = graphql(
entityId
name
defaultImage {
url: urlTemplate
url: urlTemplate(lossy: true)
altText
}
categories(first: 1) {
Expand Down
2 changes: 1 addition & 1 deletion core/components/blog-post-card/fragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const BlogPostCardFragment = graphql(`
utc
}
thumbnailImage {
url: urlTemplate
url: urlTemplate(lossy: true)
altText
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/components/footer/fragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const FooterFragment = graphql(`
}
... on StoreImageLogo {
image {
url: urlTemplate
url: urlTemplate(lossy: true)
altText
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/components/header/fragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const HeaderFragment = graphql(`
}
... on StoreImageLogo {
image {
url: urlTemplate
url: urlTemplate(lossy: true)
altText
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/components/product-card/fragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const ProductCardFragment = graphql(
name
defaultImage {
altText
url: urlTemplate
url: urlTemplate(lossy: true)
}
path
brand {
Expand Down
2 changes: 1 addition & 1 deletion core/components/store-logo/fragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const StoreLogoFragment = graphql(`
}
... on StoreImageLogo {
image {
url: urlTemplate
url: urlTemplate(lossy: true)
altText
}
}
Expand Down
14 changes: 0 additions & 14 deletions core/lib/cdn-image-loader.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
'use client';

const addCompressionParam = (url: string, lossy: boolean): string => {
const urlObj = new URL(url);

const paramValue = lossy ? 'lossy' : 'lossless';

urlObj.searchParams.set('compression', paramValue);

return urlObj.toString();
};

export default function bcCdnImageLoader({
src,
width,
height,
lossy = true,
}: {
src: string;
width: number;
height?: number;
lossy?: boolean;
}): string {
let url;

Expand All @@ -29,7 +17,5 @@ export default function bcCdnImageLoader({

url = src.replace('{:size}', `${width}w`);

url = addCompressionParam(url, lossy);

return url;
}

0 comments on commit 4d64c31

Please sign in to comment.