Skip to content

Commit 9ebbd20

Browse files
authored
Merge pull request #6451 from layer5io/leecalcote/gatsby-node-api/shouldoncreatenode
[Gatsby] onCreateNode function should check if it needs to process a node before processing it
2 parents 14c0830 + b7a0f3c commit 9ebbd20

1 file changed

Lines changed: 81 additions & 72 deletions

File tree

gatsby-node.js

Lines changed: 81 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -883,88 +883,97 @@ const onCreateChapterNode = ({ actions, node, slug }) => {
883883
createNodeField({ node, name: "pageType", value: "chapter" });
884884
};
885885

886+
// Add this helper function to determine if we should process a node
887+
const shouldOnCreateNode = ({ node }) => {
888+
return node.internal.type === "Mdx";
889+
};
890+
886891
exports.onCreateNode = ({ node, actions, getNode }) => {
892+
// Check if we should process this node
893+
if (!shouldOnCreateNode({ node })) {
894+
return;
895+
}
896+
887897
const { createNodeField } = actions;
888-
if (node.internal.type === "Mdx") {
889-
const collection = getNode(node.parent).sourceInstanceName;
898+
const collection = getNode(node.parent).sourceInstanceName;
899+
createNodeField({
900+
name: "collection",
901+
node,
902+
value: collection,
903+
});
904+
905+
if (collection !== "content-learn") {
906+
let slug = "";
907+
if (node.frontmatter.permalink) {
908+
slug = `/${collection}/${node.frontmatter.permalink}`;
909+
} else {
910+
switch (collection) {
911+
case "blog":
912+
if (node.frontmatter.published)
913+
slug = `/${collection}/${slugify(
914+
node.frontmatter.category
915+
)}/${slugify(node.frontmatter.title)}`;
916+
break;
917+
case "news":
918+
slug = `/company/${collection}/${slugify(node.frontmatter.title)}`;
919+
break;
920+
case "service-mesh-books":
921+
case "service-mesh-workshops":
922+
case "service-mesh-labs":
923+
slug = `/learn/${collection}/${slugify(node.frontmatter.title)}`;
924+
break;
925+
case "resources":
926+
if (node.frontmatter.published)
927+
slug = `/${collection}/${slugify(
928+
node.frontmatter.category
929+
)}/${slugify(node.frontmatter.title)}`;
930+
break;
931+
case "members":
932+
if (node.frontmatter.published)
933+
slug = `/community/members/${node.frontmatter.permalink ?? slugify(node.frontmatter.name)}`;
934+
break;
935+
case "events":
936+
if (node.frontmatter.title)
937+
slug = `/community/events/${slugify(node.frontmatter.title)}`;
938+
break;
939+
default:
940+
slug = `/${collection}/${slugify(node.frontmatter.title)}`;
941+
}
942+
}
890943
createNodeField({
891-
name: "collection",
944+
name: "slug",
892945
node,
893-
value: collection,
946+
value: slug,
947+
});
948+
} else {
949+
const slug = createFilePath({
950+
node,
951+
getNode,
952+
basePath: "content-learn",
953+
trailingSlash: false,
894954
});
895-
if (collection !== "content-learn") {
896-
let slug = "";
897-
if (node.frontmatter.permalink) {
898-
slug = `/${collection}/${node.frontmatter.permalink}`;
899-
} else {
900-
switch (collection) {
901-
case "blog":
902-
if (node.frontmatter.published)
903-
slug = `/${collection}/${slugify(
904-
node.frontmatter.category
905-
)}/${slugify(node.frontmatter.title)}`;
906-
break;
907-
case "news":
908-
slug = `/company/${collection}/${slugify(node.frontmatter.title)}`;
909-
break;
910-
case "service-mesh-books":
911-
case "service-mesh-workshops":
912-
case "service-mesh-labs":
913-
slug = `/learn/${collection}/${slugify(node.frontmatter.title)}`;
914-
break;
915-
case "resources":
916-
if (node.frontmatter.published)
917-
slug = `/${collection}/${slugify(
918-
node.frontmatter.category
919-
)}/${slugify(node.frontmatter.title)}`;
920-
break;
921-
case "members":
922-
if (node.frontmatter.published)
923-
slug = `/community/members/${node.frontmatter.permalink ?? slugify(node.frontmatter.name)}`;
924-
break;
925-
case "events":
926-
if (node.frontmatter.title)
927-
slug = `/community/events/${slugify(node.frontmatter.title)}`;
928-
break;
929-
default:
930-
slug = `/${collection}/${slugify(node.frontmatter.title)}`;
931-
}
932-
}
933-
createNodeField({
934-
name: "slug",
935-
node,
936-
value: slug,
937-
});
938-
} else {
939-
const slug = createFilePath({
940-
node,
941-
getNode,
942-
basePath: "content-learn",
943-
trailingSlash: false,
944-
});
945955

946-
// slug starts and ends with '/' so parts[0] and parts[-1] will be empty
947-
const parts = slug.split("/").filter((p) => !!p);
956+
// slug starts and ends with '/' so parts[0] and parts[-1] will be empty
957+
const parts = slug.split("/").filter((p) => !!p);
948958

949-
if (parts.length === 1) {
950-
onCreatePathNode({ actions, node, slug });
951-
return;
952-
}
959+
if (parts.length === 1) {
960+
onCreatePathNode({ actions, node, slug });
961+
return;
962+
}
953963

954-
if (parts.length === 2) {
955-
onCreateCourseNode({ actions, node, slug });
956-
return;
957-
}
964+
if (parts.length === 2) {
965+
onCreateCourseNode({ actions, node, slug });
966+
return;
967+
}
958968

959-
if (parts.length === 3) {
960-
onCreateSectionNode({ actions, node, slug });
961-
return;
962-
}
969+
if (parts.length === 3) {
970+
onCreateSectionNode({ actions, node, slug });
971+
return;
972+
}
963973

964-
if (parts.length === 4) {
965-
onCreateChapterNode({ actions, node, slug });
966-
return;
967-
}
974+
if (parts.length === 4) {
975+
onCreateChapterNode({ actions, node, slug });
976+
return;
968977
}
969978
}
970979
};

0 commit comments

Comments
 (0)