diff --git a/vtex/loaders/legacy/productList.ts b/vtex/loaders/legacy/productList.ts index fca73af32..ca530cd44 100644 --- a/vtex/loaders/legacy/productList.ts +++ b/vtex/loaders/legacy/productList.ts @@ -118,6 +118,12 @@ const isFQProps = (p: any): p is FQProps => isValidArrayProp(p.fq); // deno-lint-ignore no-explicit-any const isTermProps = (p: any): p is TermProps => typeof p.term === "string"; +const sanitizeNumericIds = (ids: string[] | undefined): string[] => { + return (ids || []) + .filter((id) => id && /^\d+$/.test(id.trim())) + .map((id) => id.trim()); +}; + const preferredSKU = (items: LegacyItem[], { props }: Props) => { const fetchedSkus = new Set((props as SkuIDProps).ids ?? []); return items.find((item) => fetchedSkus.has(item.itemId)) || items[0]; @@ -133,7 +139,7 @@ const fromProps = ({ props }: Props) => { }; if (isSKUIDProps(props)) { - const skuIds = props.ids || []; + const skuIds = sanitizeNumericIds(props.ids); skuIds.forEach((skuId) => params.fq.push(`skuId:${skuId}`)); params._from = 0; @@ -143,7 +149,7 @@ const fromProps = ({ props }: Props) => { } if (isProductIDProps(props)) { - const productIds = props.productIds || []; + const productIds = sanitizeNumericIds(props.productIds); productIds.forEach((productId) => params.fq.push(`productId:${productId}`)); params._from = 0; @@ -303,14 +309,12 @@ export const cacheKey = ( ]); if (isSKUIDProps(props)) { - const skuIds = [...props.ids ?? []]?.sort(); + const skuIds = sanitizeNumericIds(props.ids).sort(); params.append("skuids", skuIds.join(",")); } - if ( - isProductIDProps(props) - ) { - const productIds = [props.productIds ?? []].sort(); + if (isProductIDProps(props)) { + const productIds = sanitizeNumericIds(props.productIds).sort(); params.append("productids", productIds.join(",")); }