Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: implement shouldOnCreateNode for all our plugins/benchmarks #27545

Merged
merged 2 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 18 additions & 13 deletions benchmarks/gabe-csv-markdown/gatsby-node.js
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
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
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
@@ -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
@@ -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
@@ -1,57 +1,69 @@
const agility = require('./src/agility/utils')
pvdz marked this conversation as resolved.
Show resolved Hide resolved
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})) {
pvdz marked this conversation as resolved.
Show resolved Hide resolved
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
@@ -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
@@ -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
@@ -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
@@ -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
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.
pvdz marked this conversation as resolved.
Show resolved Hide resolved
*/
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