From 55ba62260ded8b33b5bc928ed879aadb0a32b60c Mon Sep 17 00:00:00 2001 From: Peter van der Zee Date: Wed, 14 Oct 2020 14:03:45 +0200 Subject: [PATCH 01/11] perf(gatsby): test sync before calling onCreateNode --- .../gatsby-transformer-remark/src/gatsby-node.js | 4 +++- .../src/on-node-create.js | 16 +++++++++++----- .../gatsby-transformer-sharp/src/gatsby-node.js | 4 +++- .../src/on-node-create.js | 14 ++++++++++++-- packages/gatsby/index.d.ts | 11 +++++++++++ packages/gatsby/src/utils/api-node-docs.ts | 10 ++++++++++ packages/gatsby/src/utils/api-runner-node.js | 7 +++++++ 7 files changed, 57 insertions(+), 9 deletions(-) diff --git a/packages/gatsby-transformer-remark/src/gatsby-node.js b/packages/gatsby-transformer-remark/src/gatsby-node.js index a9d2f96599009..f19fd5fdf9abb 100644 --- a/packages/gatsby-transformer-remark/src/gatsby-node.js +++ b/packages/gatsby-transformer-remark/src/gatsby-node.js @@ -1,5 +1,7 @@ +const { onCreateNode, onCreateNodeSyncTest } = require(`./on-node-create`) +exports.onCreateNode = onCreateNode +exports.onCreateNodeSyncTest = onCreateNodeSyncTest exports.createSchemaCustomization = require(`./create-schema-customization`) -exports.onCreateNode = require(`./on-node-create`) exports.setFieldsOnGraphQLNodeType = require(`./extend-node-type`) if (process.env.GATSBY_EXPERIMENTAL_PLUGIN_OPTION_VALIDATION) { diff --git a/packages/gatsby-transformer-remark/src/on-node-create.js b/packages/gatsby-transformer-remark/src/on-node-create.js index f2b2dc841213c..187bfedf0a346 100644 --- a/packages/gatsby-transformer-remark/src/on-node-create.js +++ b/packages/gatsby-transformer-remark/src/on-node-create.js @@ -1,7 +1,14 @@ const grayMatter = require(`gray-matter`) const _ = require(`lodash`) -module.exports = async function onCreateNode( +function onCreateNodeSyncTest({ node }) { + return ( + node.internal.mediaType === `text/markdown` || + node.internal.mediaType === `text/x-markdown` + ) +} + +module.exports.onCreateNode = async function onCreateNode( { node, loadNodeContent, @@ -15,10 +22,7 @@ module.exports = async function onCreateNode( const { createNode, createParentChildLink } = actions // We only care about markdown content. - if ( - node.internal.mediaType !== `text/markdown` && - node.internal.mediaType !== `text/x-markdown` - ) { + if (!onCreateNodeSyncTest({ node })) { return {} } @@ -76,3 +80,5 @@ module.exports = async function onCreateNode( return {} // eslint } } + +module.exports.onCreateNodeSyncTest = onCreateNodeSyncTest diff --git a/packages/gatsby-transformer-sharp/src/gatsby-node.js b/packages/gatsby-transformer-sharp/src/gatsby-node.js index 53ea07b51a701..e0bf3e755ccb4 100644 --- a/packages/gatsby-transformer-sharp/src/gatsby-node.js +++ b/packages/gatsby-transformer-sharp/src/gatsby-node.js @@ -1,3 +1,5 @@ -exports.onCreateNode = require(`./on-node-create`) +const { onCreateNode, onCreateNodeSyncTest } = require(`./on-node-create`) +exports.onCreateNode = onCreateNode +exports.onCreateNodeSyncTest = onCreateNodeSyncTest exports.createSchemaCustomization = require(`./customize-schema`) exports.createResolvers = require(`./create-resolvers`) diff --git a/packages/gatsby-transformer-sharp/src/on-node-create.js b/packages/gatsby-transformer-sharp/src/on-node-create.js index 7590d4fd9abc7..594986ef4df44 100644 --- a/packages/gatsby-transformer-sharp/src/on-node-create.js +++ b/packages/gatsby-transformer-sharp/src/on-node-create.js @@ -1,9 +1,17 @@ const { supportedExtensions } = require(`./supported-extensions`) -module.exports = async function onCreateNode({ node, actions, createNodeId }) { +function onCreateNodeSyncTest({ node }) { + return !!supportedExtensions[node.extension] +} + +module.exports.onCreateNode = async function onCreateNode({ + node, + actions, + createNodeId, +}) { const { createNode, createParentChildLink } = actions - if (!supportedExtensions[node.extension]) { + if (!onCreateNodeSyncTest({ node })) { return } @@ -22,3 +30,5 @@ module.exports = async function onCreateNode({ node, actions, createNodeId }) { return } + +module.exports.onCreateNodeSyncTest = onCreateNodeSyncTest diff --git a/packages/gatsby/index.d.ts b/packages/gatsby/index.d.ts index b8a448defead3..2dc51608fc8be 100644 --- a/packages/gatsby/index.d.ts +++ b/packages/gatsby/index.d.ts @@ -306,6 +306,17 @@ export interface GatsbyNode { callback?: PluginCallback ): void + /** + * Called before scheduling a `onCreateNode` callback for a plugin. If it returns falsy + * then Gatsby will not schedule the `onCreateNode` callback for this node for this plugin. + * + * @example + * exports.onCreateNodeSyncTest = node => node.internal.type === 'SharpNode' + */ + onCreateNodeSyncTest?( + node: TNode, + ): boolean + /** * Called when a new page is created. This extension API is useful * for programmatically manipulating pages created by other plugins e.g. diff --git a/packages/gatsby/src/utils/api-node-docs.ts b/packages/gatsby/src/utils/api-node-docs.ts index 37937babd48a9..0c5eb537c818d 100644 --- a/packages/gatsby/src/utils/api-node-docs.ts +++ b/packages/gatsby/src/utils/api-node-docs.ts @@ -133,6 +133,16 @@ export const sourceNodes = true */ export const onCreateNode = true +/** + * Called when a new node is created before the `onCreateNode` handler is called. + * This is an optimization that can prevent the `onCreateNode` handler to be scheduled + * if the plugin already knows it's not interested in processing this node. + * + * @example + * exports.onCreateNodeSyncTest = (node) => node.internal.type === 'Image' + */ +export const onCreateNodeSyncTest = true + /** * Called when a new page is created. This extension API is useful * for programmatically manipulating pages created by other plugins e.g. diff --git a/packages/gatsby/src/utils/api-runner-node.js b/packages/gatsby/src/utils/api-runner-node.js index 43ca6a498ef8c..3bdfa3b926843 100644 --- a/packages/gatsby/src/utils/api-runner-node.js +++ b/packages/gatsby/src/utils/api-runner-node.js @@ -530,6 +530,13 @@ module.exports = async (api, args = {}, { pluginSource, activity } = {}) => const pluginName = plugin.name === `default-site-plugin` ? `gatsby-node.js` : plugin.name + // if (api === 'onCreateNode') { + // if (plugin.nodeAPIs.includes('onCreateNodeSyncTest') && !require(plugin.resolve + '/gatsby-node.js')?.onCreateNodeSyncTest(args)) { + // // Do not try to schedule an async event for this node for this plugin + // return null; + // } + // } + return new Promise(resolve => { resolve(runAPI(plugin, api, { ...args, parentSpan: apiSpan }, activity)) }).catch(err => { From 109fdfc83a872b2d310fdeb526e5fb139c34490f Mon Sep 17 00:00:00 2001 From: Peter van der Zee Date: Wed, 14 Oct 2020 14:40:22 +0200 Subject: [PATCH 02/11] Dont wrap node in object, fix tests --- .../src/__tests__/on-node-create.js | 2 +- .../src/on-node-create.js | 4 ++-- .../src/on-node-create.js | 4 ++-- packages/gatsby/src/utils/api-runner-node.js | 19 +++++++++++++------ 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/packages/gatsby-transformer-remark/src/__tests__/on-node-create.js b/packages/gatsby-transformer-remark/src/__tests__/on-node-create.js index 7a085101225d6..4663c596d9059 100644 --- a/packages/gatsby-transformer-remark/src/__tests__/on-node-create.js +++ b/packages/gatsby-transformer-remark/src/__tests__/on-node-create.js @@ -1,6 +1,6 @@ const Promise = require(`bluebird`) const _ = require(`lodash`) -const onCreateNode = require(`../on-node-create`) +const { onCreateNode } = require(`../on-node-create`) const { graphql } = require(`gatsby/graphql`) const { createContentDigest } = require(`gatsby-core-utils`) diff --git a/packages/gatsby-transformer-remark/src/on-node-create.js b/packages/gatsby-transformer-remark/src/on-node-create.js index 187bfedf0a346..ba0e0a03a37a4 100644 --- a/packages/gatsby-transformer-remark/src/on-node-create.js +++ b/packages/gatsby-transformer-remark/src/on-node-create.js @@ -1,7 +1,7 @@ const grayMatter = require(`gray-matter`) const _ = require(`lodash`) -function onCreateNodeSyncTest({ node }) { +function onCreateNodeSyncTest(node) { return ( node.internal.mediaType === `text/markdown` || node.internal.mediaType === `text/x-markdown` @@ -22,7 +22,7 @@ module.exports.onCreateNode = async function onCreateNode( const { createNode, createParentChildLink } = actions // We only care about markdown content. - if (!onCreateNodeSyncTest({ node })) { + if (!onCreateNodeSyncTest(node)) { return {} } diff --git a/packages/gatsby-transformer-sharp/src/on-node-create.js b/packages/gatsby-transformer-sharp/src/on-node-create.js index 594986ef4df44..f86a08beb1027 100644 --- a/packages/gatsby-transformer-sharp/src/on-node-create.js +++ b/packages/gatsby-transformer-sharp/src/on-node-create.js @@ -1,6 +1,6 @@ const { supportedExtensions } = require(`./supported-extensions`) -function onCreateNodeSyncTest({ node }) { +function onCreateNodeSyncTest(node) { return !!supportedExtensions[node.extension] } @@ -11,7 +11,7 @@ module.exports.onCreateNode = async function onCreateNode({ }) { const { createNode, createParentChildLink } = actions - if (!onCreateNodeSyncTest({ node })) { + if (!onCreateNodeSyncTest(node)) { return } diff --git a/packages/gatsby/src/utils/api-runner-node.js b/packages/gatsby/src/utils/api-runner-node.js index 3bdfa3b926843..887a723d063f7 100644 --- a/packages/gatsby/src/utils/api-runner-node.js +++ b/packages/gatsby/src/utils/api-runner-node.js @@ -527,15 +527,22 @@ module.exports = async (api, args = {}, { pluginSource, activity } = {}) => return null } + let gatsbyNode = pluginNodeCache.get(plugin.name) + if (!gatsbyNode) { + gatsbyNode = require(`${plugin.resolve}/gatsby-node`) + pluginNodeCache.set(plugin.name, gatsbyNode) + } + const pluginName = plugin.name === `default-site-plugin` ? `gatsby-node.js` : plugin.name - // if (api === 'onCreateNode') { - // if (plugin.nodeAPIs.includes('onCreateNodeSyncTest') && !require(plugin.resolve + '/gatsby-node.js')?.onCreateNodeSyncTest(args)) { - // // Do not try to schedule an async event for this node for this plugin - // return null; - // } - // } + if ( + api === `onCreateNode` && + gatsbyNode?.onCreateNodeSyncTest(args.node) + ) { + // Do not try to schedule an async event for this node for this plugin + return null + } return new Promise(resolve => { resolve(runAPI(plugin, api, { ...args, parentSpan: apiSpan }, activity)) From dcb611be71fbf60e1275bdff332c4e7411b26407 Mon Sep 17 00:00:00 2001 From: Peter van der Zee Date: Wed, 14 Oct 2020 14:58:06 +0200 Subject: [PATCH 03/11] This snapshot does not fail locally but does on CI --- packages/gatsby/scripts/__tests__/api.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/gatsby/scripts/__tests__/api.js b/packages/gatsby/scripts/__tests__/api.js index a105112d831d5..7d55443946bf3 100644 --- a/packages/gatsby/scripts/__tests__/api.js +++ b/packages/gatsby/scripts/__tests__/api.js @@ -46,6 +46,7 @@ it("generates the expected api output", done => { "onCreateBabelConfig": Object {}, "onCreateDevServer": Object {}, "onCreateNode": Object {}, + "onCreateNodeSyncTest": Object {}, "onCreatePage": Object {}, "onCreateWebpackConfig": Object {}, "onPostBootstrap": Object {}, From 01e2eab2fcca86d5b4993dd73eb886b35d8062a3 Mon Sep 17 00:00:00 2001 From: Peter van der Zee Date: Wed, 14 Oct 2020 15:18:50 +0200 Subject: [PATCH 04/11] Must check if value exists too, not just plugin --- packages/gatsby/src/utils/api-runner-node.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/gatsby/src/utils/api-runner-node.js b/packages/gatsby/src/utils/api-runner-node.js index 887a723d063f7..85418ca4e624b 100644 --- a/packages/gatsby/src/utils/api-runner-node.js +++ b/packages/gatsby/src/utils/api-runner-node.js @@ -538,7 +538,7 @@ module.exports = async (api, args = {}, { pluginSource, activity } = {}) => if ( api === `onCreateNode` && - gatsbyNode?.onCreateNodeSyncTest(args.node) + gatsbyNode?.onCreateNodeSyncTest?.(args.node) ) { // Do not try to schedule an async event for this node for this plugin return null From 4cddee5879ab83cb2622030cdfbfd5e29849bdd3 Mon Sep 17 00:00:00 2001 From: Peter van der Zee Date: Thu, 15 Oct 2020 13:00:40 +0200 Subject: [PATCH 05/11] Disarm the footgun --- packages/gatsby/src/utils/api-runner-node.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/gatsby/src/utils/api-runner-node.js b/packages/gatsby/src/utils/api-runner-node.js index 85418ca4e624b..f696909f4166a 100644 --- a/packages/gatsby/src/utils/api-runner-node.js +++ b/packages/gatsby/src/utils/api-runner-node.js @@ -538,7 +538,8 @@ module.exports = async (api, args = {}, { pluginSource, activity } = {}) => if ( api === `onCreateNode` && - gatsbyNode?.onCreateNodeSyncTest?.(args.node) + gatsbyNode?.onCreateNodeSyncTest && // Don't bail if this api is not exported + !gatsbyNode.onCreateNodeSyncTest(args.node) ) { // Do not try to schedule an async event for this node for this plugin return null From 812ecceaf13f82714298eae8ae41821266178282 Mon Sep 17 00:00:00 2001 From: Peter van der Zee Date: Thu, 15 Oct 2020 15:00:11 +0200 Subject: [PATCH 06/11] Apply feedback --- packages/gatsby-transformer-remark/src/gatsby-node.js | 4 ++-- packages/gatsby-transformer-remark/src/on-node-create.js | 6 +++--- packages/gatsby-transformer-sharp/src/gatsby-node.js | 4 ++-- packages/gatsby-transformer-sharp/src/on-node-create.js | 6 +++--- packages/gatsby/index.d.ts | 4 ++-- packages/gatsby/scripts/__tests__/api.js | 2 +- packages/gatsby/src/utils/api-node-docs.ts | 5 +++-- packages/gatsby/src/utils/api-runner-node.js | 5 +++-- 8 files changed, 19 insertions(+), 17 deletions(-) diff --git a/packages/gatsby-transformer-remark/src/gatsby-node.js b/packages/gatsby-transformer-remark/src/gatsby-node.js index f19fd5fdf9abb..97d50d35857db 100644 --- a/packages/gatsby-transformer-remark/src/gatsby-node.js +++ b/packages/gatsby-transformer-remark/src/gatsby-node.js @@ -1,6 +1,6 @@ -const { onCreateNode, onCreateNodeSyncTest } = require(`./on-node-create`) +const { onCreateNode, shouldOnCreateNode } = require(`./on-node-create`) exports.onCreateNode = onCreateNode -exports.onCreateNodeSyncTest = onCreateNodeSyncTest +exports.shouldOnCreateNode = shouldOnCreateNode exports.createSchemaCustomization = require(`./create-schema-customization`) exports.setFieldsOnGraphQLNodeType = require(`./extend-node-type`) diff --git a/packages/gatsby-transformer-remark/src/on-node-create.js b/packages/gatsby-transformer-remark/src/on-node-create.js index ba0e0a03a37a4..f8fad4797fb28 100644 --- a/packages/gatsby-transformer-remark/src/on-node-create.js +++ b/packages/gatsby-transformer-remark/src/on-node-create.js @@ -1,7 +1,7 @@ const grayMatter = require(`gray-matter`) const _ = require(`lodash`) -function onCreateNodeSyncTest(node) { +function shouldOnCreateNode(node) { return ( node.internal.mediaType === `text/markdown` || node.internal.mediaType === `text/x-markdown` @@ -22,7 +22,7 @@ module.exports.onCreateNode = async function onCreateNode( const { createNode, createParentChildLink } = actions // We only care about markdown content. - if (!onCreateNodeSyncTest(node)) { + if (!shouldOnCreateNode(node)) { return {} } @@ -81,4 +81,4 @@ module.exports.onCreateNode = async function onCreateNode( } } -module.exports.onCreateNodeSyncTest = onCreateNodeSyncTest +module.exports.shouldOnCreateNode = shouldOnCreateNode diff --git a/packages/gatsby-transformer-sharp/src/gatsby-node.js b/packages/gatsby-transformer-sharp/src/gatsby-node.js index e0bf3e755ccb4..22e30bcbfcc45 100644 --- a/packages/gatsby-transformer-sharp/src/gatsby-node.js +++ b/packages/gatsby-transformer-sharp/src/gatsby-node.js @@ -1,5 +1,5 @@ -const { onCreateNode, onCreateNodeSyncTest } = require(`./on-node-create`) +const { onCreateNode, shouldOnCreateNode } = require(`./on-node-create`) exports.onCreateNode = onCreateNode -exports.onCreateNodeSyncTest = onCreateNodeSyncTest +exports.shouldOnCreateNode = shouldOnCreateNode exports.createSchemaCustomization = require(`./customize-schema`) exports.createResolvers = require(`./create-resolvers`) diff --git a/packages/gatsby-transformer-sharp/src/on-node-create.js b/packages/gatsby-transformer-sharp/src/on-node-create.js index f86a08beb1027..2d75b0a97f440 100644 --- a/packages/gatsby-transformer-sharp/src/on-node-create.js +++ b/packages/gatsby-transformer-sharp/src/on-node-create.js @@ -1,6 +1,6 @@ const { supportedExtensions } = require(`./supported-extensions`) -function onCreateNodeSyncTest(node) { +function shouldOnCreateNode(node) { return !!supportedExtensions[node.extension] } @@ -11,7 +11,7 @@ module.exports.onCreateNode = async function onCreateNode({ }) { const { createNode, createParentChildLink } = actions - if (!onCreateNodeSyncTest(node)) { + if (!shouldOnCreateNode(node)) { return } @@ -31,4 +31,4 @@ module.exports.onCreateNode = async function onCreateNode({ return } -module.exports.onCreateNodeSyncTest = onCreateNodeSyncTest +module.exports.shouldOnCreateNode = shouldOnCreateNode diff --git a/packages/gatsby/index.d.ts b/packages/gatsby/index.d.ts index 2dc51608fc8be..9199cafa4007c 100644 --- a/packages/gatsby/index.d.ts +++ b/packages/gatsby/index.d.ts @@ -311,9 +311,9 @@ export interface GatsbyNode { * then Gatsby will not schedule the `onCreateNode` callback for this node for this plugin. * * @example - * exports.onCreateNodeSyncTest = node => node.internal.type === 'SharpNode' + * exports.shouldOnCreateNode = node => node.internal.type === 'SharpNode' */ - onCreateNodeSyncTest?( + shouldOnCreateNode?( node: TNode, ): boolean diff --git a/packages/gatsby/scripts/__tests__/api.js b/packages/gatsby/scripts/__tests__/api.js index 7d55443946bf3..051a8934b0cb4 100644 --- a/packages/gatsby/scripts/__tests__/api.js +++ b/packages/gatsby/scripts/__tests__/api.js @@ -46,7 +46,7 @@ it("generates the expected api output", done => { "onCreateBabelConfig": Object {}, "onCreateDevServer": Object {}, "onCreateNode": Object {}, - "onCreateNodeSyncTest": Object {}, + "shouldOnCreateNode": Object {}, "onCreatePage": Object {}, "onCreateWebpackConfig": Object {}, "onPostBootstrap": Object {}, diff --git a/packages/gatsby/src/utils/api-node-docs.ts b/packages/gatsby/src/utils/api-node-docs.ts index 0c5eb537c818d..66193d57bb415 100644 --- a/packages/gatsby/src/utils/api-node-docs.ts +++ b/packages/gatsby/src/utils/api-node-docs.ts @@ -138,10 +138,11 @@ export const onCreateNode = true * This is an optimization that can prevent the `onCreateNode` handler to be scheduled * if the plugin already knows it's not interested in processing this node. * + * @gatsbyVersion 2.24.78 * @example - * exports.onCreateNodeSyncTest = (node) => node.internal.type === 'Image' + * exports.shouldOnCreateNode = (node) => node.internal.type === 'Image' */ -export const onCreateNodeSyncTest = true +export const shouldOnCreateNode = true /** * Called when a new page is created. This extension API is useful diff --git a/packages/gatsby/src/utils/api-runner-node.js b/packages/gatsby/src/utils/api-runner-node.js index f696909f4166a..7ff57e6ca7277 100644 --- a/packages/gatsby/src/utils/api-runner-node.js +++ b/packages/gatsby/src/utils/api-runner-node.js @@ -536,10 +536,11 @@ module.exports = async (api, args = {}, { pluginSource, activity } = {}) => const pluginName = plugin.name === `default-site-plugin` ? `gatsby-node.js` : plugin.name + // TODO: rethink createNode API to handle this better if ( api === `onCreateNode` && - gatsbyNode?.onCreateNodeSyncTest && // Don't bail if this api is not exported - !gatsbyNode.onCreateNodeSyncTest(args.node) + gatsbyNode?.shouldOnCreateNode && // Don't bail if this api is not exported + !gatsbyNode.shouldOnCreateNode(args.node) ) { // Do not try to schedule an async event for this node for this plugin return null From b91528c4edd8f59bba8ce95ceef96027451d3d5a Mon Sep 17 00:00:00 2001 From: Peter van der Zee Date: Thu, 15 Oct 2020 16:04:59 +0200 Subject: [PATCH 07/11] Update version and related snapshot --- packages/gatsby/scripts/__tests__/api.js | 4 +++- packages/gatsby/src/utils/api-node-docs.ts | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/gatsby/scripts/__tests__/api.js b/packages/gatsby/scripts/__tests__/api.js index 051a8934b0cb4..ef90801d52e45 100644 --- a/packages/gatsby/scripts/__tests__/api.js +++ b/packages/gatsby/scripts/__tests__/api.js @@ -46,7 +46,6 @@ it("generates the expected api output", done => { "onCreateBabelConfig": Object {}, "onCreateDevServer": Object {}, "onCreateNode": Object {}, - "shouldOnCreateNode": Object {}, "onCreatePage": Object {}, "onCreateWebpackConfig": Object {}, "onPostBootstrap": Object {}, @@ -59,6 +58,9 @@ it("generates the expected api output", done => { "preprocessSource": Object {}, "resolvableExtensions": Object {}, "setFieldsOnGraphQLNodeType": Object {}, + "shouldOnCreateNode": Object { + "version": "2.24.79", + }, "sourceNodes": Object {}, }, "ssr": Object { diff --git a/packages/gatsby/src/utils/api-node-docs.ts b/packages/gatsby/src/utils/api-node-docs.ts index 66193d57bb415..705dcd77b3973 100644 --- a/packages/gatsby/src/utils/api-node-docs.ts +++ b/packages/gatsby/src/utils/api-node-docs.ts @@ -138,7 +138,7 @@ export const onCreateNode = true * This is an optimization that can prevent the `onCreateNode` handler to be scheduled * if the plugin already knows it's not interested in processing this node. * - * @gatsbyVersion 2.24.78 + * @gatsbyVersion 2.24.79 * @example * exports.shouldOnCreateNode = (node) => node.internal.type === 'Image' */ From 823afb952f9caa300c8a2c0e8ef1166620487be2 Mon Sep 17 00:00:00 2001 From: Peter van der Zee Date: Thu, 15 Oct 2020 17:11:08 +0200 Subject: [PATCH 08/11] Add `pluginOptions` as second callback param. Add note about api. --- packages/gatsby/index.d.ts | 4 +++- packages/gatsby/src/utils/api-node-docs.ts | 8 ++++---- packages/gatsby/src/utils/api-runner-node.js | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/gatsby/index.d.ts b/packages/gatsby/index.d.ts index 9199cafa4007c..39ed24b4db943 100644 --- a/packages/gatsby/index.d.ts +++ b/packages/gatsby/index.d.ts @@ -309,9 +309,11 @@ export interface GatsbyNode { /** * Called before scheduling a `onCreateNode` callback for a plugin. If it returns falsy * then Gatsby will not schedule the `onCreateNode` callback for this node for this plugin. + * Note: this API does not receive the regular `api` that other callbacks get as first arg. * + * @gatsbyVersion 2.24.79 * @example - * exports.shouldOnCreateNode = node => node.internal.type === 'SharpNode' + * exports.shouldOnCreateNode = (node, pluginOptions) => node.internal.type === 'Image' */ shouldOnCreateNode?( node: TNode, diff --git a/packages/gatsby/src/utils/api-node-docs.ts b/packages/gatsby/src/utils/api-node-docs.ts index 705dcd77b3973..ebc907636c081 100644 --- a/packages/gatsby/src/utils/api-node-docs.ts +++ b/packages/gatsby/src/utils/api-node-docs.ts @@ -134,13 +134,13 @@ export const sourceNodes = true export const onCreateNode = true /** - * Called when a new node is created before the `onCreateNode` handler is called. - * This is an optimization that can prevent the `onCreateNode` handler to be scheduled - * if the plugin already knows it's not interested in processing this node. + * Called before scheduling a `onCreateNode` callback for a plugin. If it returns falsy + * then Gatsby will not schedule the `onCreateNode` callback for this node for this plugin. + * Note: this API does not receive the regular `api` that other callbacks get as first arg. * * @gatsbyVersion 2.24.79 * @example - * exports.shouldOnCreateNode = (node) => node.internal.type === 'Image' + * exports.shouldOnCreateNode = (node, pluginOptions) => node.internal.type === 'Image' */ export const shouldOnCreateNode = true diff --git a/packages/gatsby/src/utils/api-runner-node.js b/packages/gatsby/src/utils/api-runner-node.js index 7ff57e6ca7277..313d413e36f0c 100644 --- a/packages/gatsby/src/utils/api-runner-node.js +++ b/packages/gatsby/src/utils/api-runner-node.js @@ -540,7 +540,7 @@ module.exports = async (api, args = {}, { pluginSource, activity } = {}) => if ( api === `onCreateNode` && gatsbyNode?.shouldOnCreateNode && // Don't bail if this api is not exported - !gatsbyNode.shouldOnCreateNode(args.node) + !gatsbyNode.shouldOnCreateNode(args.node, plugin.pluginOptions) ) { // Do not try to schedule an async event for this node for this plugin return null From e4979b48f91e9830cb4f2757414d2156b6fc482e Mon Sep 17 00:00:00 2001 From: Peter van der Zee Date: Fri, 16 Oct 2020 11:22:41 +0200 Subject: [PATCH 09/11] shouldOnCreateNode -> unstable_shouldOnCreateNode --- packages/gatsby-transformer-remark/src/gatsby-node.js | 7 +++++-- packages/gatsby-transformer-remark/src/on-node-create.js | 6 +++--- packages/gatsby-transformer-sharp/src/gatsby-node.js | 7 +++++-- packages/gatsby-transformer-sharp/src/on-node-create.js | 6 +++--- packages/gatsby/index.d.ts | 6 +++--- packages/gatsby/scripts/__tests__/api.js | 4 ++-- packages/gatsby/src/utils/api-node-docs.ts | 6 +++--- packages/gatsby/src/utils/api-runner-node.js | 4 ++-- 8 files changed, 26 insertions(+), 20 deletions(-) diff --git a/packages/gatsby-transformer-remark/src/gatsby-node.js b/packages/gatsby-transformer-remark/src/gatsby-node.js index 97d50d35857db..7330ef19ea39c 100644 --- a/packages/gatsby-transformer-remark/src/gatsby-node.js +++ b/packages/gatsby-transformer-remark/src/gatsby-node.js @@ -1,6 +1,9 @@ -const { onCreateNode, shouldOnCreateNode } = require(`./on-node-create`) +const { + onCreateNode, + unstable_shouldOnCreateNode, +} = require(`./on-node-create`) exports.onCreateNode = onCreateNode -exports.shouldOnCreateNode = shouldOnCreateNode +exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode exports.createSchemaCustomization = require(`./create-schema-customization`) exports.setFieldsOnGraphQLNodeType = require(`./extend-node-type`) diff --git a/packages/gatsby-transformer-remark/src/on-node-create.js b/packages/gatsby-transformer-remark/src/on-node-create.js index f8fad4797fb28..d2680f2824d50 100644 --- a/packages/gatsby-transformer-remark/src/on-node-create.js +++ b/packages/gatsby-transformer-remark/src/on-node-create.js @@ -1,7 +1,7 @@ const grayMatter = require(`gray-matter`) const _ = require(`lodash`) -function shouldOnCreateNode(node) { +function unstable_shouldOnCreateNode(node) { return ( node.internal.mediaType === `text/markdown` || node.internal.mediaType === `text/x-markdown` @@ -22,7 +22,7 @@ module.exports.onCreateNode = async function onCreateNode( const { createNode, createParentChildLink } = actions // We only care about markdown content. - if (!shouldOnCreateNode(node)) { + if (!unstable_shouldOnCreateNode(node)) { return {} } @@ -81,4 +81,4 @@ module.exports.onCreateNode = async function onCreateNode( } } -module.exports.shouldOnCreateNode = shouldOnCreateNode +module.exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode diff --git a/packages/gatsby-transformer-sharp/src/gatsby-node.js b/packages/gatsby-transformer-sharp/src/gatsby-node.js index 22e30bcbfcc45..81be7827f5a02 100644 --- a/packages/gatsby-transformer-sharp/src/gatsby-node.js +++ b/packages/gatsby-transformer-sharp/src/gatsby-node.js @@ -1,5 +1,8 @@ -const { onCreateNode, shouldOnCreateNode } = require(`./on-node-create`) +const { + onCreateNode, + unstable_shouldOnCreateNode, +} = require(`./on-node-create`) exports.onCreateNode = onCreateNode -exports.shouldOnCreateNode = shouldOnCreateNode +exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode exports.createSchemaCustomization = require(`./customize-schema`) exports.createResolvers = require(`./create-resolvers`) diff --git a/packages/gatsby-transformer-sharp/src/on-node-create.js b/packages/gatsby-transformer-sharp/src/on-node-create.js index 2d75b0a97f440..34cb9c41c1565 100644 --- a/packages/gatsby-transformer-sharp/src/on-node-create.js +++ b/packages/gatsby-transformer-sharp/src/on-node-create.js @@ -1,6 +1,6 @@ const { supportedExtensions } = require(`./supported-extensions`) -function shouldOnCreateNode(node) { +function unstable_shouldOnCreateNode(node) { return !!supportedExtensions[node.extension] } @@ -11,7 +11,7 @@ module.exports.onCreateNode = async function onCreateNode({ }) { const { createNode, createParentChildLink } = actions - if (!shouldOnCreateNode(node)) { + if (!unstable_shouldOnCreateNode(node)) { return } @@ -31,4 +31,4 @@ module.exports.onCreateNode = async function onCreateNode({ return } -module.exports.shouldOnCreateNode = shouldOnCreateNode +module.exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode diff --git a/packages/gatsby/index.d.ts b/packages/gatsby/index.d.ts index 39ed24b4db943..6e39974835afd 100644 --- a/packages/gatsby/index.d.ts +++ b/packages/gatsby/index.d.ts @@ -311,11 +311,11 @@ export interface GatsbyNode { * then Gatsby will not schedule the `onCreateNode` callback for this node for this plugin. * Note: this API does not receive the regular `api` that other callbacks get as first arg. * - * @gatsbyVersion 2.24.79 + * @gatsbyVersion 2.24.80 * @example - * exports.shouldOnCreateNode = (node, pluginOptions) => node.internal.type === 'Image' + * exports.unstable_shouldOnCreateNode = (node, pluginOptions) => node.internal.type === 'Image' */ - shouldOnCreateNode?( + unstable_shouldOnCreateNode?( node: TNode, ): boolean diff --git a/packages/gatsby/scripts/__tests__/api.js b/packages/gatsby/scripts/__tests__/api.js index ef90801d52e45..21040b2451ad6 100644 --- a/packages/gatsby/scripts/__tests__/api.js +++ b/packages/gatsby/scripts/__tests__/api.js @@ -58,8 +58,8 @@ it("generates the expected api output", done => { "preprocessSource": Object {}, "resolvableExtensions": Object {}, "setFieldsOnGraphQLNodeType": Object {}, - "shouldOnCreateNode": Object { - "version": "2.24.79", + "unstable_shouldOnCreateNode": Object { + "version": "2.24.80", }, "sourceNodes": Object {}, }, diff --git a/packages/gatsby/src/utils/api-node-docs.ts b/packages/gatsby/src/utils/api-node-docs.ts index ebc907636c081..4e723047bbec4 100644 --- a/packages/gatsby/src/utils/api-node-docs.ts +++ b/packages/gatsby/src/utils/api-node-docs.ts @@ -138,11 +138,11 @@ export const onCreateNode = true * then Gatsby will not schedule the `onCreateNode` callback for this node for this plugin. * Note: this API does not receive the regular `api` that other callbacks get as first arg. * - * @gatsbyVersion 2.24.79 + * @gatsbyVersion 2.24.80 * @example - * exports.shouldOnCreateNode = (node, pluginOptions) => node.internal.type === 'Image' + * exports.unstable_shouldOnCreateNode = (node, pluginOptions) => node.internal.type === 'Image' */ -export const shouldOnCreateNode = true +export const unstable_shouldOnCreateNode = true /** * Called when a new page is created. This extension API is useful diff --git a/packages/gatsby/src/utils/api-runner-node.js b/packages/gatsby/src/utils/api-runner-node.js index 313d413e36f0c..0ba4041a602ce 100644 --- a/packages/gatsby/src/utils/api-runner-node.js +++ b/packages/gatsby/src/utils/api-runner-node.js @@ -539,8 +539,8 @@ module.exports = async (api, args = {}, { pluginSource, activity } = {}) => // TODO: rethink createNode API to handle this better if ( api === `onCreateNode` && - gatsbyNode?.shouldOnCreateNode && // Don't bail if this api is not exported - !gatsbyNode.shouldOnCreateNode(args.node, plugin.pluginOptions) + gatsbyNode?.unstable_shouldOnCreateNode && // Don't bail if this api is not exported + !gatsbyNode.unstable_shouldOnCreateNode(args.node, plugin.pluginOptions) ) { // Do not try to schedule an async event for this node for this plugin return null From 7c065f04dd1ef7500de8e64db0a7bacd946ab302 Mon Sep 17 00:00:00 2001 From: Peter van der Zee Date: Fri, 16 Oct 2020 11:49:36 +0200 Subject: [PATCH 10/11] it would help if these snapshots were the same locally. --- packages/gatsby/scripts/__tests__/api.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/gatsby/scripts/__tests__/api.js b/packages/gatsby/scripts/__tests__/api.js index 21040b2451ad6..105f3c9ab04f9 100644 --- a/packages/gatsby/scripts/__tests__/api.js +++ b/packages/gatsby/scripts/__tests__/api.js @@ -58,10 +58,10 @@ it("generates the expected api output", done => { "preprocessSource": Object {}, "resolvableExtensions": Object {}, "setFieldsOnGraphQLNodeType": Object {}, + "sourceNodes": Object {}, "unstable_shouldOnCreateNode": Object { "version": "2.24.80", }, - "sourceNodes": Object {}, }, "ssr": Object { "onPreRenderHTML": Object {}, From 75a2488d87c050c92c32e8c93662debf15c6a2c1 Mon Sep 17 00:00:00 2001 From: Peter van der Zee Date: Fri, 16 Oct 2020 12:28:35 +0200 Subject: [PATCH 11/11] Also update the first arg to be `{node}` instead of `node` --- packages/gatsby-transformer-remark/src/on-node-create.js | 4 ++-- packages/gatsby-transformer-sharp/src/on-node-create.js | 4 ++-- packages/gatsby/index.d.ts | 5 +++-- packages/gatsby/src/utils/api-node-docs.ts | 2 +- packages/gatsby/src/utils/api-runner-node.js | 5 ++++- 5 files changed, 12 insertions(+), 8 deletions(-) diff --git a/packages/gatsby-transformer-remark/src/on-node-create.js b/packages/gatsby-transformer-remark/src/on-node-create.js index d2680f2824d50..b47dce011f945 100644 --- a/packages/gatsby-transformer-remark/src/on-node-create.js +++ b/packages/gatsby-transformer-remark/src/on-node-create.js @@ -1,7 +1,7 @@ const grayMatter = require(`gray-matter`) const _ = require(`lodash`) -function unstable_shouldOnCreateNode(node) { +function unstable_shouldOnCreateNode({ node }) { return ( node.internal.mediaType === `text/markdown` || node.internal.mediaType === `text/x-markdown` @@ -22,7 +22,7 @@ module.exports.onCreateNode = async function onCreateNode( const { createNode, createParentChildLink } = actions // We only care about markdown content. - if (!unstable_shouldOnCreateNode(node)) { + if (!unstable_shouldOnCreateNode({ node })) { return {} } diff --git a/packages/gatsby-transformer-sharp/src/on-node-create.js b/packages/gatsby-transformer-sharp/src/on-node-create.js index 34cb9c41c1565..30d319187cbf3 100644 --- a/packages/gatsby-transformer-sharp/src/on-node-create.js +++ b/packages/gatsby-transformer-sharp/src/on-node-create.js @@ -1,6 +1,6 @@ const { supportedExtensions } = require(`./supported-extensions`) -function unstable_shouldOnCreateNode(node) { +function unstable_shouldOnCreateNode({ node }) { return !!supportedExtensions[node.extension] } @@ -11,7 +11,7 @@ module.exports.onCreateNode = async function onCreateNode({ }) { const { createNode, createParentChildLink } = actions - if (!unstable_shouldOnCreateNode(node)) { + if (!unstable_shouldOnCreateNode({ node })) { return } diff --git a/packages/gatsby/index.d.ts b/packages/gatsby/index.d.ts index 6e39974835afd..5337a4d438fc0 100644 --- a/packages/gatsby/index.d.ts +++ b/packages/gatsby/index.d.ts @@ -313,10 +313,11 @@ export interface GatsbyNode { * * @gatsbyVersion 2.24.80 * @example - * exports.unstable_shouldOnCreateNode = (node, pluginOptions) => node.internal.type === 'Image' + * exports.unstable_shouldOnCreateNode = ({node}, pluginOptions) => node.internal.type === 'Image' */ unstable_shouldOnCreateNode?( - node: TNode, + args: { node: TNode }, + options?: PluginOptions ): boolean /** diff --git a/packages/gatsby/src/utils/api-node-docs.ts b/packages/gatsby/src/utils/api-node-docs.ts index 4e723047bbec4..7513afdc58f93 100644 --- a/packages/gatsby/src/utils/api-node-docs.ts +++ b/packages/gatsby/src/utils/api-node-docs.ts @@ -140,7 +140,7 @@ export const onCreateNode = true * * @gatsbyVersion 2.24.80 * @example - * exports.unstable_shouldOnCreateNode = (node, pluginOptions) => node.internal.type === 'Image' + * exports.unstable_shouldOnCreateNode = ({node}, pluginOptions) => node.internal.type === 'Image' */ export const unstable_shouldOnCreateNode = true diff --git a/packages/gatsby/src/utils/api-runner-node.js b/packages/gatsby/src/utils/api-runner-node.js index 0ba4041a602ce..b4ea0c5e95b02 100644 --- a/packages/gatsby/src/utils/api-runner-node.js +++ b/packages/gatsby/src/utils/api-runner-node.js @@ -540,7 +540,10 @@ module.exports = async (api, args = {}, { pluginSource, activity } = {}) => if ( api === `onCreateNode` && gatsbyNode?.unstable_shouldOnCreateNode && // Don't bail if this api is not exported - !gatsbyNode.unstable_shouldOnCreateNode(args.node, plugin.pluginOptions) + !gatsbyNode.unstable_shouldOnCreateNode( + { node: args.node }, + plugin.pluginOptions + ) ) { // Do not try to schedule an async event for this node for this plugin return null