Skip to content

Commit

Permalink
perf: implement shouldOnCreateNode for all our plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdz committed Oct 19, 2020
1 parent 47d8041 commit d497458
Show file tree
Hide file tree
Showing 30 changed files with 386 additions and 210 deletions.
31 changes: 18 additions & 13 deletions benchmarks/gabe-csv-markdown/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,26 @@ exports.createPages = async ({ graphql, actions }) => {
})
}

function unstable_shouldOnCreateNode({ node }) {
return node.internal.type === `GendataCsv`
}

// Not sure if there is a better way than to create a proxy node for markdown to pick up
// I certainly can't get remark to to pick up csv nodes :(
exports.onCreateNode = ({ node, actions }) => {
function onCreateNode({ node, actions }) {
const { createNode } = actions

if (node.internal.type === `GendataCsv`) {
createNode({
id: `${node.id}-MarkdownProxy`,
parent: node.id,
internal: {
type: `MarkdownProxy`,
mediaType: "text/markdown",
content: node.articleContent,
contentDigest: node.articleContent,
},
})
}
createNode({
id: `${node.id}-MarkdownProxy`,
parent: node.id,
internal: {
type: `MarkdownProxy`,
mediaType: "text/markdown",
content: node.articleContent,
contentDigest: node.articleContent,
},
})
}

exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode
exports.onCreateNode = onCreateNode
23 changes: 14 additions & 9 deletions benchmarks/markdown_id/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,20 @@ exports.createPages = async ({ graphql, actions }) => {
})
}

exports.onCreateNode = ({ node, actions, getNode }) => {
function unstable_shouldOnCreateNode({ node }) {
return node.internal.type === `MarkdownRemark`
}

function onCreateNode({ node, actions, getNode }) {
const { createNodeField } = actions

if (node.internal.type === `MarkdownRemark`) {
const value = createFilePath({ node, getNode })
createNodeField({
name: `slug`,
node,
value,
})
}
const value = createFilePath({ node, getNode })
createNodeField({
name: `slug`,
node,
value,
})
}

exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode
exports.onCreateNode = onCreateNode
23 changes: 14 additions & 9 deletions benchmarks/markdown_slug/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,20 @@ exports.createPages = async ({ graphql, actions }) => {
})
}

exports.onCreateNode = ({ node, actions, getNode }) => {
function unstable_shouldOnCreateNode({ node }) {
return node.internal.type === `MarkdownRemark`
}

function onCreateNode({ node, actions, getNode }) {
const { createNodeField } = actions

if (node.internal.type === `MarkdownRemark`) {
const value = createFilePath({ node, getNode })
createNodeField({
name: `slug`,
node,
value,
})
}
const value = createFilePath({ node, getNode })
createNodeField({
name: `slug`,
node,
value,
})
}

exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode
exports.onCreateNode = onCreateNode
32 changes: 18 additions & 14 deletions benchmarks/md/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,6 @@
const path = require("path")
const { createFilePath } = require("gatsby-source-filesystem")

exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions

if (node.internal.type === "MarkdownRemark") {
const value = createFilePath({ node, getNode })

createNodeField({
name: "path",
node,
value,
})
}
}

exports.createPages = async ({ graphql, actions, reporter }) => {
const { createPage } = actions

Expand Down Expand Up @@ -47,3 +33,21 @@ exports.createPages = async ({ graphql, actions, reporter }) => {
})
})
}

function unstable_shouldOnCreateNode({ node }) {
return node.internal.type === `MarkdownRemark`
}

function onCreateNode({ node, actions, getNode }) {
const { createNodeField } = actions

const value = createFilePath({ node, getNode })
createNodeField({
name: `path`,
node,
value,
})
}

exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode
exports.onCreateNode = onCreateNode
18 changes: 10 additions & 8 deletions benchmarks/mdx/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
const path = require("path")
const { createFilePath } = require("gatsby-source-filesystem")

exports.unstable_shouldOnCreateNode = function ({ node }) {
return node.internal.type === "Mdx"
}

exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions

if (node.internal.type === "Mdx") {
const value = createFilePath({ node, getNode })
const value = createFilePath({ node, getNode })

createNodeField({
name: "path",
node,
value,
})
}
createNodeField({
name: "path",
node,
value,
})
}

exports.createPages = async ({ graphql, actions, reporter }) => {
Expand Down
100 changes: 56 additions & 44 deletions benchmarks/source-agilitycms/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,69 @@
const agility = require('./src/agility/utils')
const agility = require("./src/agility/utils")
const { createRemoteFileNode } = require("gatsby-source-filesystem")

//gatsy-node.js
//CREATE RESOLVERS *******************************************************************************************
exports.createResolvers = (args) => {
const {
createResolvers,
getNode,
createNodeId,
createNode,
createContentDigest,
configOptions,
} = args

const { createResolvers, getNode, createNodeId, createNode, createContentDigest, configOptions } = args;
const resolvers = {
//on the 'agilityPost' node type...
agilityPost: {
//get the sitemap node that represents this item - useful for retrieving the URL for the item
sitemapNode: agility.getDynamicPageItemSitemapNode(),

const resolvers = {
//on the 'agilityPost' node type...
agilityPost: {
//get the sitemap node that represents this item - useful for retrieving the URL for the item
sitemapNode: agility.getDynamicPageItemSitemapNode(),
//[Not Implemented]
//if we had a linked content field for 'author', this is how we'd get the author for this post in a single GraphQl query
//linkedContent_agilityAuthor: agility.getLinkedContentItem({ type: 'agilityAuthor', linkedContentFieldName: 'author' })
},

//[Not Implemented]
//if we had a linked content field for 'author', this is how we'd get the author for this post in a single GraphQl query
//linkedContent_agilityAuthor: agility.getLinkedContentItem({ type: 'agilityAuthor', linkedContentFieldName: 'author' })
},
//[Not Implemented]
//if we had an 'Image Slider' module and it had a list of slides via a linked content field called 'slides', this is how we'd retrieve a list of those slides in a single GraphQL query
// agilityImageSlider: {
// linkedContent_agilitySlides: agility.getLinkedContentList({ type: 'agilitySlide', linkedContentFieldName: 'slides' })
// }
}
createResolvers(resolvers)
}

//[Not Implemented]
//if we had an 'Image Slider' module and it had a list of slides via a linked content field called 'slides', this is how we'd retrieve a list of those slides in a single GraphQL query
// agilityImageSlider: {
// linkedContent_agilitySlides: agility.getLinkedContentList({ type: 'agilitySlide', linkedContentFieldName: 'slides' })
// }
}
createResolvers(resolvers)
function unstable_shouldOnCreateNode({node}) {
return (
node.properties && node.properties.referenceName.toLowerCase() === `posts`
)
}

exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode

exports.onCreateNode = async ({
node,
actions: { createNode },
store,
cache,
createNodeId,
node,
actions: { createNode },
store,
cache,
createNodeId,
}) => {

if (node.properties
&& node.properties.referenceName.toLowerCase() === `posts`) {
let field = "image"
let imageUrl = node.customFields[field]
if (imageUrl) {
let fileNode = await createRemoteFileNode({
url: imageUrl, // string that points to the URL of the image
parentNodeId: node.id, // id of the parent node of the fileNode you are going to create
createNode, // helper function in gatsby-node to generate the node
createNodeId, // helper function in gatsby-node to generate the node id
cache, // Gatsby's cache
store, // Gatsby's redux store
})
// if the file was created, attach the new node to the parent node
if (fileNode) {
node.customFields[`${field}LocalImg___NODE`] = fileNode.id
}
}
}
}
if (unstable_shouldOnCreateNode({node})) {
let field = "image"
let imageUrl = node.customFields[field]
if (imageUrl) {
let fileNode = await createRemoteFileNode({
url: imageUrl, // string that points to the URL of the image
parentNodeId: node.id, // id of the parent node of the fileNode you are going to create
createNode, // helper function in gatsby-node to generate the node
createNodeId, // helper function in gatsby-node to generate the node id
cache, // Gatsby's cache
store, // Gatsby's redux store
})
// if the file was created, attach the new node to the parent node
if (fileNode) {
node.customFields[`${field}LocalImg___NODE`] = fileNode.id
}
}
}
}
14 changes: 10 additions & 4 deletions benchmarks/source-cosmicjs/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
const kebabCase = require(`lodash.kebabcase`)

function unstable_shouldOnCreateNode({node}) {
return node.internal.type === "node__article"
}

exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode

exports.onCreateNode = ({ actions, node }) => {
const { createNodeField } = actions

if (node.internal.type === 'node__article') {
if (unstable_shouldOnCreateNode({node})) {
createNodeField({ node, name: "slug", value: kebabCase(node.title) })
}
}
Expand All @@ -27,13 +33,13 @@ exports.createPages = async ({ actions, graphql, reporter }) => {
reporter.panicOnBuild(result.errors)
}

result.data.articles.edges.map(article => {
result.data.articles.edges.map((article) => {
createPage({
path: article.node.slug,
component: require.resolve(`./src/templates/article.js`),
context: {
slug: article.node.slug,
}
},
})
})
}
}
8 changes: 7 additions & 1 deletion benchmarks/source-datocms/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
const kebabCase = require(`lodash.kebabcase`)

function unstable_shouldOnCreateNode({node}) {
return node.internal.type === `node__article`
}

exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode

exports.onCreateNode = ({ actions, node }) => {
const { createNodeField } = actions

if (node.internal.type === `node__article`) {
if (unstable_shouldOnCreateNode({node})) {
createNodeField({ node, name: `slug`, value: kebabCase(node.title) })
}
}
Expand Down
8 changes: 7 additions & 1 deletion benchmarks/source-drupal/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
const kebabCase = require(`lodash.kebabcase`)

function unstable_shouldOnCreateNode({node}) {
return node.internal.type === `node__article`
}

exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode

exports.onCreateNode = ({ actions, node }) => {
const { createNodeField } = actions

if (node.internal.type === "node__article") {
if (unstable_shouldOnCreateNode({node})) {
createNodeField({ node, name: "slug", value: kebabCase(node.title) })
}
}
Expand Down
8 changes: 7 additions & 1 deletion benchmarks/source-flotiq/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
const kebabCase = require(`lodash.kebabcase`)

function unstable_shouldOnCreateNode({node}) {
return node.internal.type === `node__article`
}

exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode

exports.onCreateNode = ({ actions, node }) => {
const { createNodeField } = actions

if (node.internal.type === "node__article") {
if (unstable_shouldOnCreateNode({node})) {
createNodeField({ node, name: "slug", value: kebabCase(node.title) })
}
}
Expand Down
12 changes: 11 additions & 1 deletion packages/gatsby-plugin-mdx/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,25 @@ const { MDX_SCOPES_LOCATION } = require(`./constants`)
const defaultOptions = require(`./utils/default-options`)
const fs = require(`fs`)

const {
onCreateNode,
unstable_shouldOnCreateNode
} = require(`./gatsby/on-create-node`)

/**
* Create Mdx types and resolvers
*/
exports.sourceNodes = require(`./gatsby/source-nodes`)

/**
* Check whether the Create Mdx nodes from MDX files.
*/
exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode

/**
* Create Mdx nodes from MDX files.
*/
exports.onCreateNode = require(`./gatsby/on-create-node`)
exports.onCreateNode = onCreateNode

/**
* Add frontmatter as page context for MDX pages
Expand Down

0 comments on commit d497458

Please sign in to comment.