@@ -15,8 +15,14 @@ function createChunkFromProduct(
1515 baseUrl : string ,
1616 crawlOptions : ExtendedCrawlOptions ,
1717) : ChunkReqPayload {
18- // Extract image URLs
19- const imageUrls = product . media . nodes . map ( ( media ) => media . preview . image . url ) ;
18+ const imageUrls : string [ ] = [
19+ ...( product ?. media ?. nodes
20+ ?. map ( ( media ) => media ?. preview ?. image ?. url )
21+ . filter ( ( url ) : url is string => ! ! url ) ?? [ ] ) ,
22+ ...( product ?. images
23+ ?. map ( ( image ) => image ?. src )
24+ . filter ( ( url ) : url is string => ! ! url ) ?? [ ] ) ,
25+ ] ;
2026
2127 // Handle text cleaning
2228 let productTitle = product . title || "" ;
@@ -73,6 +79,7 @@ function createChunkFromProduct(
7379 const semanticBoostPhrase = groupVariants ? variantTitle : productTitle ;
7480 const fulltextBoostPhrase = groupVariants ? variantTitle : productTitle ;
7581 const tags = product . tags ;
82+ tags . push ( ...variantTitle . split ( " / " ) ) ;
7683
7784 if ( crawlOptions . include_metafields ) {
7885 product . variants . nodes . forEach ( ( v ) => {
@@ -84,15 +91,12 @@ function createChunkFromProduct(
8491 tags . push ( ...values ) ;
8592 } ) ;
8693 }
87- tags . push ( ...variantTitle . split ( " / " ) ) ;
8894 const metadata = {
8995 body_html : product . bodyHtml ,
9096 variantName : variantTitle ,
9197 handle : product . handle ,
9298 id : parseInt ( product . id . split ( "/" ) . pop ( ) || "0" ) ,
93- images : product . media . nodes . map ( ( media ) => ( {
94- src : media . preview . image . url ,
95- } ) ) ,
99+ images : imageUrls ,
96100 tags : product . tags ,
97101 status : product . status ,
98102 title : product . title ,
@@ -147,8 +151,14 @@ export function createChunkFromProductWebhook(
147151 baseUrl : string ,
148152 crawlOptions : ExtendedCrawlOptions ,
149153) : ChunkReqPayload {
150- // Extract image URLs
151- const imageUrls = product . media . map ( ( media ) => media . preview . image . url ) ;
154+ const imageUrls : string [ ] = [
155+ ...( product ?. media
156+ ?. map ( ( media ) => media ?. preview ?. image ?. url )
157+ . filter ( ( url ) : url is string => ! ! url ) ?? [ ] ) ,
158+ ...( product ?. images
159+ ?. map ( ( image ) => image ?. src )
160+ . filter ( ( url ) : url is string => ! ! url ) ?? [ ] ) ,
161+ ] ;
152162
153163 // Handle text cleaning
154164 let productTitle = product . title || "" ;
@@ -176,14 +186,19 @@ export function createChunkFromProductWebhook(
176186 variant . title === "Default Title"
177187 ? `<h1>${ productTitle } </h1>${ productBodyHtml } `
178188 : `<h1>${ productTitle } - ${ variantTitle } </h1>${ productBodyHtml } ` ;
189+ let tags = product . tags ;
190+ if ( typeof tags === "string" ) {
191+ tags = tags . split ( "," ) . map ( ( tag ) => tag . trim ( ) ) ;
192+ }
193+ tags . push ( ...variantTitle . split ( " / " ) ) ;
179194
180195 if ( crawlOptions . scrape_options ?. tag_regexes ) {
181196 const tagMatches = new Set < string > ( ) ;
182197
183198 crawlOptions . scrape_options . tag_regexes . forEach ( ( pattern ) => {
184199 try {
185200 const regex = new RegExp ( pattern ) ;
186- product . tags . forEach ( ( tag ) => {
201+ tags . forEach ( ( tag ) => {
187202 if ( regex . test ( tag ) ) {
188203 tagMatches . add ( `<span>${ pattern } </span>` ) ;
189204 }
@@ -204,7 +219,6 @@ export function createChunkFromProductWebhook(
204219 const semanticBoostPhrase = groupVariants ? variantTitle : productTitle ;
205220 const fulltextBoostPhrase = groupVariants ? variantTitle : productTitle ;
206221
207- const tags = product . tags ;
208222 if ( crawlOptions . include_metafields ) {
209223 product . variants . forEach ( ( v ) => {
210224 let values : string [ ] = JSON . parse (
@@ -242,9 +256,9 @@ export function createChunkFromProductWebhook(
242256 tag_set : tags ,
243257 num_value : parseFloat ( variant . price ) ,
244258 metadata,
245- tracking_id : groupVariants ? variant . id : product . id ,
246- group_tracking_ids : groupVariants ? [ product . id ] : undefined ,
247- image_urls : imageUrls ,
259+ tracking_id : groupVariants ? variant . id . toString ( ) : product . id . toString ( ) ,
260+ group_tracking_ids : groupVariants ? [ product . id . toString ( ) ] : undefined ,
261+ image_urls : imageUrls ?? [ ] ,
248262 fulltext_boost : {
249263 phrase : fulltextBoostPhrase ,
250264 boost_factor : 1.3 ,
@@ -284,15 +298,17 @@ export async function sendChunksFromWebhook(
284298 if ( response . error ) {
285299 throw response . error ;
286300 }
287- let data = response . data as {
288- data : {
289- productVariant ?: {
290- metafields : { nodes : { key : string ; value : string } [ ] } ;
301+ let data = (
302+ response as {
303+ data : {
304+ productVariant ?: {
305+ metafields : { nodes : { key : string ; value : string } [ ] } ;
306+ } ;
291307 } ;
292- } ;
293- } ;
308+ }
309+ ) . data ;
294310
295- variant . metafields = data ?. data . productVariant ?. metafields . nodes ?? [ ] ;
311+ variant . metafields = data ?. productVariant ?. metafields ? .nodes ?? [ ] ;
296312 return createChunkFromProductWebhook (
297313 product ,
298314 variant ,
@@ -411,8 +427,8 @@ export const sendChunks = async (
411427 if ( response . error ) {
412428 throw response . error ;
413429 }
414- const dataChunks : ChunkReqPayload [ ] = response . data . products . nodes
415- . flatMap ( ( product ) =>
430+ const dataChunks : ChunkReqPayload [ ] = response . data . products . nodes . flatMap (
431+ ( product ) =>
416432 product . variants . nodes . map ( ( variant ) =>
417433 createChunkFromProduct (
418434 product ,
@@ -421,7 +437,7 @@ export const sendChunks = async (
421437 crawlOptions ,
422438 ) ,
423439 ) ,
424- ) ;
440+ ) ;
425441
426442 for ( const batch of chunk_to_size ( dataChunks , 120 ) ) {
427443 const sendPromise = sendChunksToTrieve ( batch , key , datasetId ?? "" ) ;
0 commit comments