Algolia Search Re-Rendering With Next.JS & Shopify
Implementation :
The frontend is built with Next.js, while Shopify serves as the backend. Algolia's react-instantsearch library powers product data display on Product Listing Pages (PLPs). The collection.js component fetches product data from Algolia and passes it to the ProductItemTile.js component for rendering. Each PLP displays approximately 102–104 products per page. To supplement incomplete data from Algolia, a get-plp-product-data API retrieves additional product details from Shopify within the ProductItemTile.js component.
Challenge :
Reloading the PLP triggers multiple get-plp-product-data API calls, exceeding the number of products on the page for all pages except the first.
Debugging
Check if the get-plp-product-data API call is inside a useEffect. If it is, ensure the dependency array is correct.
useEffect(() => {
fetchProductDetails();
}, []); // Empty dependency array might still cause issues if props change unexpectedly.
Verify Unique Identifiers
{products.map((product) => (
<ProductItemTile key={product.id} product={product} />
))}
Memoization with React.memo or useMemo
export default React.memo(ProductItemTile);
Or memoizing computed product data:
const productData = useMemo(() => {
return calculateProductData();
}, [dependencies]);
Optimize API Call in ProductItemTile.js
const productData = await Promise.all(
products.map(product => fetch(`/api/get-plp-product-data?id=${product.id}`).then(res => res.json()))
);
领英推荐
Pagination-Specific Behavior
Solution :
Refactor API Call to Parent Component
const fetchProductData = async (products) => {
const productDetails = await Promise.all(
products.map(product => fetch(`/api/get-plp-product-data?id=${product.id}`).then(res => res.json()))
);
return productDetails;
};
useEffect(() => {
fetchProductData(algoliaProducts).then((details) => {
setDetailedProducts(details);
});
}, [algoliaProducts]);
Use React.memo for ProductItemTile.js
const ProductItemTile = React.memo(({ product }) => {
// Component logic
return <div>{product.name}</div>;
});
Ensure Stable Keys
Pagination Optimization
Let me know if you need further guidance or specific code examples for implementation!
Looking to Build or Upgrade Your E-commerce Store?
?? Contact us today at [email protected]
?? Visit us at: https://w3nuts.co.uk/