Skip to content

Commit 06646dd

Browse files
feat: [WIP] add image optimizations
1 parent b66bd18 commit 06646dd

File tree

1 file changed

+200
-5
lines changed

1 file changed

+200
-5
lines changed

src/gatsby-node.js

Lines changed: 200 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"use strict";
33

44
import { randomUUID } from "crypto";
5+
import { createRemoteFileNode } from "gatsby-source-filesystem";
56
import _ from "lodash";
67
import {
78
ACCESS_CONTROL_ALLOW_CREDENTIALS,
@@ -240,7 +241,7 @@ exports.sourceNodes = async ({ actions: { createNode }, cache, createNodeId, cre
240241
// Check if the data was retrieved successfully
241242
if (status === "fulfilled" && !isEmpty(nodeName) && !isEmpty(endpoint) && !isEmpty(data)) {
242243
// Handle camel casing of the data
243-
const updatedData = handleCamelizeKeys(data);
244+
const updatedData = await handleCamelizeKeys(data);
244245

245246
// Create nodes from the data
246247
if (isArrayType(updatedData)) {
@@ -259,6 +260,8 @@ exports.sourceNodes = async ({ actions: { createNode }, cache, createNodeId, cre
259260
// Cache the data
260261
await cache.set(CACHE_KEY, sourceData).catch((err) => console.error(`[ERROR] ${err?.message} || ${convertObjectToString(err)} || "An error occurred while caching the data. Please try again later."`));
261262

263+
console.info(`${APP_NAME} task processing complete!`);
264+
262265
// Resolve the promise
263266
return sourceData;
264267
};
@@ -297,15 +300,38 @@ exports.sourceNodes = async ({ actions: { createNode }, cache, createNodeId, cre
297300
// Resolve the promise
298301
return {
299302
nodeName,
300-
data: results || null,
303+
data: !isEmpty(results) && !isEmpty(nodeName) ? (nodeName === "OptimizelyStoreContent" || nodeName === "OptimizelyHotelContent" ? results?.Locations : results) : null,
301304
endpoint
302305
};
303306
}) || null
304307
)
305308
.then(async (res) => {
306309
// Store the data in the cache
307310
if (!isEmpty(res)) {
308-
sourceData = res;
311+
const tempRes = res;
312+
313+
tempRes.map(async (item) => {
314+
const tempItem = item;
315+
316+
const {
317+
status = null,
318+
value: { nodeName = null, data = null, endpoint = null }
319+
} = tempItem;
320+
321+
if (status === "fulfilled" && !isEmpty(nodeName) && !isEmpty(endpoint) && !isEmpty(data)) {
322+
if (nodeName === "OptimizelyStoreContent") {
323+
tempItem.value.data = data?.filter(({ LocationType }) => LocationType === "Store");
324+
} else if (nodeName === "OptimizelyHotelContent") {
325+
tempItem.value.data = data?.filter(({ LocationType }) => LocationType === "Hotel");
326+
}
327+
}
328+
329+
item = tempItem;
330+
331+
return item;
332+
});
333+
334+
sourceData = tempRes;
309335
}
310336

311337
// Create nodes from the cached data
@@ -329,8 +355,6 @@ exports.sourceNodes = async ({ actions: { createNode }, cache, createNodeId, cre
329355
console.error(`[AUTH] API authentication failed. Please check your credentials and try again.`);
330356
}
331357

332-
console.info(`${APP_NAME} task processing complete!`);
333-
334358
return;
335359
};
336360

@@ -368,3 +392,174 @@ exports.createSchemaCustomization = ({ actions: { createTypes } }, pluginOptions
368392

369393
return;
370394
};
395+
396+
/**
397+
* @description Perform image optimizations on each node created
398+
* @param {Object} node
399+
* @param {Object} actions
400+
* @param {Object} store
401+
* @param {Object} cache
402+
* @param {Object} reporter
403+
* @returns {void}
404+
*/
405+
exports.onCreateNode = async ({ node, actions: { createNode, createNodeField }, getCache }) => {
406+
if (node.internal.type.includes("Optimizely")) {
407+
const contentBlocks = ["contentBlocks", "contentBlocksTop", "contentBlocksBottom"];
408+
const blockImageElementObjects = ["image", "secondary", "image1", "image2", "headerImage", "listImage", "secondaryListImage", "topImage"];
409+
const blockImageElementArrays = ["images", "items", "productImages"];
410+
411+
Object.keys(node)?.map(async (key) => {
412+
await Promise.all(
413+
contentBlocks
414+
?.filter((contentBlock) => contentBlock?.includes(key))
415+
?.map(() => {
416+
node?.[key]?.map((block) => {
417+
const {
418+
contentLink: { expanded }
419+
} = block;
420+
421+
Object.keys(expanded)?.map((expandedKey) => {
422+
// Check for keys that contain image objects
423+
blockImageElementObjects
424+
?.filter((imageElement) => expandedKey === imageElement)
425+
?.map(async () => {
426+
await createRemoteFileNode({
427+
url:
428+
expanded?.[expandedKey]?.contentLink?.expanded?.contentLink?.url ||
429+
expanded?.[expandedKey]?.contentLink?.expanded?.url ||
430+
expanded?.[expandedKey]?.expanded?.url ||
431+
expanded?.[expandedKey]?.url ||
432+
expanded?.[expandedKey],
433+
parentNodeId: node.id,
434+
createNode,
435+
createNodeId: () =>
436+
`${node.id}-${key}-items--${
437+
expanded?.[expandedKey]?.contentLink?.expanded?.contentLink?.url ||
438+
expanded?.[expandedKey]?.contentLink?.expanded?.url ||
439+
expanded?.[expandedKey]?.expanded?.url ||
440+
expanded?.[expandedKey]?.url ||
441+
expanded?.[expandedKey]
442+
}`,
443+
getCache
444+
});
445+
});
446+
447+
// Check for keys that contain image objects
448+
blockImageElementArrays
449+
?.filter((imageElement) => expandedKey === imageElement)
450+
?.map(() => {
451+
expanded?.[expandedKey]?.map(async (image) => {
452+
await Promise.allSettled(
453+
blockImageElementObjects
454+
?.filter((imageElement) => Object.keys(image)?.includes(imageElement))
455+
?.map(
456+
async (imageElement) =>
457+
await createRemoteFileNode({
458+
url:
459+
image?.[imageElement]?.contentLink?.expanded?.contentLink?.url ||
460+
image?.[imageElement]?.contentLink?.expanded?.url ||
461+
image?.[imageElement]?.expanded?.url ||
462+
image?.[imageElement]?.url ||
463+
image?.[imageElement],
464+
parentNodeId: node.id,
465+
createNode,
466+
createNodeId: () =>
467+
`${node.id}-${key}-items--${
468+
image?.[imageElement]?.contentLink?.expanded?.contentLink?.url ||
469+
image?.[imageElement]?.contentLink?.expanded?.url ||
470+
image?.[imageElement]?.expanded?.url ||
471+
image?.[imageElement]?.url ||
472+
image?.[imageElement]
473+
}`,
474+
getCache
475+
})
476+
)
477+
)
478+
.then((res) => {
479+
if (res && Array.isArray(res) && res?.length > 0) {
480+
const fields = [];
481+
482+
res?.map((field) => fields.push(field.id));
483+
484+
createNodeField({
485+
node,
486+
name: `localFile`,
487+
value: fields
488+
});
489+
}
490+
491+
return res;
492+
})
493+
.catch((err) => {
494+
console.error(err);
495+
return err;
496+
});
497+
});
498+
499+
return expanded?.[expandedKey];
500+
});
501+
502+
return expanded;
503+
});
504+
505+
return block;
506+
});
507+
508+
return node;
509+
})
510+
);
511+
512+
await Promise.all(
513+
blockImageElementObjects
514+
?.filter((imageElement) => key === imageElement)
515+
?.map(async () => {
516+
await createRemoteFileNode({
517+
url: node[key]?.contentLink?.expanded?.contentLink?.url || node[key]?.expanded?.contentLink?.url || node[key]?.expanded?.url || node[key]?.url || node[key],
518+
parentNodeId: node.id,
519+
createNode,
520+
createNodeId: () => `${node.id}-${key}--${node[key]?.contentLink?.expanded?.contentLink?.url || node[key]?.expanded?.contentLink?.url || node[key]?.expanded?.url || node[key]?.url || node[key]}`,
521+
getCache
522+
});
523+
})
524+
);
525+
526+
await Promise.all(
527+
blockImageElementArrays
528+
?.filter((imageElement) => key === imageElement)
529+
?.map(async () => {
530+
await Promise.all(
531+
node?.[key]?.map(
532+
async (element) =>
533+
await createRemoteFileNode({
534+
url: element?.contentLink?.expanded?.contentLink?.url || element?.expanded?.contentLink?.url || element?.expanded?.url || element?.url || element,
535+
parentNodeId: node.id,
536+
createNode,
537+
createNodeId: () => `
538+
${node.id}-${key}-items--${element?.contentLink?.expanded?.contentLink?.url || element?.expanded?.contentLink?.url || element?.expanded?.url || element?.url || element}`,
539+
getCache
540+
})
541+
)
542+
)
543+
.then((res) => {
544+
if (res && Array.isArray(res) && res?.length > 0) {
545+
const fields = [];
546+
547+
res?.map((field) => fields.push(field.id));
548+
549+
createNodeField({
550+
node,
551+
name: `localFile`,
552+
value: fields
553+
});
554+
}
555+
556+
return res;
557+
})
558+
.catch((err) => err);
559+
560+
return node;
561+
})
562+
);
563+
});
564+
}
565+
};

0 commit comments

Comments
 (0)