Skip to content

Commit 14c85a7

Browse files
authored
Merge pull request #35 from deco-sites/jonasjesus/deco-5272-prefetch-toggle
feat(plp): admin toggle for product-link prefetch (DECO-5272)
2 parents 663d771 + 763e81e commit 14c85a7

4 files changed

Lines changed: 35 additions & 4 deletions

File tree

src/components/product/card/ProductCard.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ export interface Props {
1919
product: Product;
2020
/** Preload card image */
2121
preload?: boolean;
22+
/**
23+
* Router preload strategy for the product link — "intent" prefetches the PDP
24+
* on hover/focus; false disables it. (Distinct from `preload`, the image LCP
25+
* flag.)
26+
*/
27+
prefetch?: "intent" | false;
2228
/** @description used for analytics event */
2329
itemListName?: string;
2430
/** @description index of the product card in the list */
@@ -34,6 +40,7 @@ const SHOE_SIZE_VARIANT = "shoe size";
3440
export default function ProductCard({
3541
product,
3642
preload,
43+
prefetch = "intent",
3744
itemListName,
3845
index,
3946
className,
@@ -105,6 +112,7 @@ export default function ProductCard({
105112
width={WIDTH}
106113
height={HEIGHT}
107114
preload={preload}
115+
prefetch={prefetch}
108116
inStock={inStock}
109117
/>
110118
)}
@@ -133,7 +141,7 @@ export default function ProductCard({
133141
</div>
134142
</figure>
135143

136-
<Link to={selectedHref} preload="intent" className="pt-5">
144+
<Link to={selectedHref} preload={prefetch} className="pt-5">
137145
<ProductCardTitle title={title} />
138146
<ProductCardPrice
139147
price={price}

src/components/product/card/ProductCardImage.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ export interface Props {
1010
backAlt?: string;
1111
width: number;
1212
height: number;
13+
/** Eager-load the image (LCP) — distinct from `prefetch` (route preload). */
1314
preload?: boolean;
15+
/** Router preload strategy for the product link. */
16+
prefetch?: "intent" | false;
1417
inStock: boolean;
1518
}
1619

@@ -23,13 +26,14 @@ export default function ProductCardImage({
2326
width,
2427
height,
2528
preload,
29+
prefetch = "intent",
2630
inStock,
2731
}: Props) {
2832
const aspectRatio = `${width} / ${height}`;
2933
return (
3034
<Link
3135
to={href}
32-
preload="intent"
36+
preload={prefetch}
3337
aria-label="view product"
3438
className={clx(
3539
"absolute top-0 left-0",

src/components/search/SearchResult.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ export interface Layout {
2424
* @default "show-more"
2525
*/
2626
pagination?: "show-more" | "pagination";
27+
/**
28+
* @title Pré-carregar página do produto
29+
* @description Quando ativado, a página do produto começa a carregar assim
30+
* que o usuário passa o mouse sobre o card, deixando a navegação mais rápida.
31+
* Pode aumentar o consumo de dados em listas muito grandes.
32+
* @default true
33+
*/
34+
enablePrefetch?: boolean;
2735
}
2836

2937
export interface Props {
@@ -122,7 +130,13 @@ function Result({
122130

123131
{isRouteLoading
124132
? <SearchResultGridSkeleton count={Math.min(perPage, 12) || 8} />
125-
: <SearchResultGrid products={products} offset={offset} />}
133+
: (
134+
<SearchResultGrid
135+
products={products}
136+
offset={offset}
137+
prefetch={layout?.enablePrefetch === false ? false : "intent"}
138+
/>
139+
)}
126140

127141
<div className="grid place-items-center pt-2 sm:pt-10">
128142
<SearchPagination

src/components/search/SearchResultGrid.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ export interface Props {
66
products: Product[];
77
/** Index offset of the first product in the slice (used for analytics) */
88
offset?: number;
9+
/** Router preload strategy passed to each card's product link. */
10+
prefetch?: "intent" | false;
911
}
1012

11-
export default function SearchResultGrid({ products, offset = 0 }: Props) {
13+
export default function SearchResultGrid(
14+
{ products, offset = 0, prefetch = "intent" }: Props,
15+
) {
1216
return (
1317
<div
1418
data-product-list
@@ -24,6 +28,7 @@ export default function SearchResultGrid({ products, offset = 0 }: Props) {
2428
key={`product-card-${product.productID}`}
2529
product={product}
2630
preload={index === 0}
31+
prefetch={prefetch}
2732
index={offset + index}
2833
className="h-full min-w-40 max-w-75"
2934
/>

0 commit comments

Comments
 (0)