Skip to content

Commit 6400383

Browse files
authoredOct 16, 2020
perf(gatsby): test sync before calling onCreateNode (#27442)
* perf(gatsby): test sync before calling onCreateNode * Dont wrap node in object, fix tests * This snapshot does not fail locally but does on CI * Must check if value exists too, not just plugin * Disarm the footgun * Apply feedback * Update version and related snapshot * Add `pluginOptions` as second callback param. Add note about api. * shouldOnCreateNode -> unstable_shouldOnCreateNode * it would help if these snapshots were the same locally. * Also update the first arg to be `{node}` instead of `node`
1 parent f227e85 commit 6400383

File tree

9 files changed

+83
-10
lines changed

9 files changed

+83
-10
lines changed
 

‎packages/gatsby-transformer-remark/src/__tests__/on-node-create.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const Promise = require(`bluebird`)
22
const _ = require(`lodash`)
3-
const onCreateNode = require(`../on-node-create`)
3+
const { onCreateNode } = require(`../on-node-create`)
44
const { graphql } = require(`gatsby/graphql`)
55

66
const { createContentDigest } = require(`gatsby-core-utils`)

‎packages/gatsby-transformer-remark/src/gatsby-node.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
const {
2+
onCreateNode,
3+
unstable_shouldOnCreateNode,
4+
} = require(`./on-node-create`)
5+
exports.onCreateNode = onCreateNode
6+
exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode
17
exports.createSchemaCustomization = require(`./create-schema-customization`)
2-
exports.onCreateNode = require(`./on-node-create`)
38
exports.setFieldsOnGraphQLNodeType = require(`./extend-node-type`)
49

510
if (process.env.GATSBY_EXPERIMENTAL_PLUGIN_OPTION_VALIDATION) {

‎packages/gatsby-transformer-remark/src/on-node-create.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
const grayMatter = require(`gray-matter`)
22
const _ = require(`lodash`)
33

4-
module.exports = async function onCreateNode(
4+
function unstable_shouldOnCreateNode({ node }) {
5+
return (
6+
node.internal.mediaType === `text/markdown` ||
7+
node.internal.mediaType === `text/x-markdown`
8+
)
9+
}
10+
11+
module.exports.onCreateNode = async function onCreateNode(
512
{
613
node,
714
loadNodeContent,
@@ -15,10 +22,7 @@ module.exports = async function onCreateNode(
1522
const { createNode, createParentChildLink } = actions
1623

1724
// We only care about markdown content.
18-
if (
19-
node.internal.mediaType !== `text/markdown` &&
20-
node.internal.mediaType !== `text/x-markdown`
21-
) {
25+
if (!unstable_shouldOnCreateNode({ node })) {
2226
return {}
2327
}
2428

@@ -76,3 +80,5 @@ module.exports = async function onCreateNode(
7680
return {} // eslint
7781
}
7882
}
83+
84+
module.exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1-
exports.onCreateNode = require(`./on-node-create`)
1+
const {
2+
onCreateNode,
3+
unstable_shouldOnCreateNode,
4+
} = require(`./on-node-create`)
5+
exports.onCreateNode = onCreateNode
6+
exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode
27
exports.createSchemaCustomization = require(`./customize-schema`)
38
exports.createResolvers = require(`./create-resolvers`)

‎packages/gatsby-transformer-sharp/src/on-node-create.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
const { supportedExtensions } = require(`./supported-extensions`)
22

3-
module.exports = async function onCreateNode({ node, actions, createNodeId }) {
3+
function unstable_shouldOnCreateNode({ node }) {
4+
return !!supportedExtensions[node.extension]
5+
}
6+
7+
module.exports.onCreateNode = async function onCreateNode({
8+
node,
9+
actions,
10+
createNodeId,
11+
}) {
412
const { createNode, createParentChildLink } = actions
513

6-
if (!supportedExtensions[node.extension]) {
14+
if (!unstable_shouldOnCreateNode({ node })) {
715
return
816
}
917

@@ -22,3 +30,5 @@ module.exports = async function onCreateNode({ node, actions, createNodeId }) {
2230

2331
return
2432
}
33+
34+
module.exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode

‎packages/gatsby/index.d.ts

+14
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,20 @@ export interface GatsbyNode {
306306
callback?: PluginCallback
307307
): void
308308

309+
/**
310+
* Called before scheduling a `onCreateNode` callback for a plugin. If it returns falsy
311+
* then Gatsby will not schedule the `onCreateNode` callback for this node for this plugin.
312+
* Note: this API does not receive the regular `api` that other callbacks get as first arg.
313+
*
314+
* @gatsbyVersion 2.24.80
315+
* @example
316+
* exports.unstable_shouldOnCreateNode = ({node}, pluginOptions) => node.internal.type === 'Image'
317+
*/
318+
unstable_shouldOnCreateNode?<TNode extends object = {}>(
319+
args: { node: TNode },
320+
options?: PluginOptions
321+
): boolean
322+
309323
/**
310324
* Called when a new page is created. This extension API is useful
311325
* for programmatically manipulating pages created by other plugins e.g.

‎packages/gatsby/scripts/__tests__/api.js

+3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ it("generates the expected api output", done => {
5959
"resolvableExtensions": Object {},
6060
"setFieldsOnGraphQLNodeType": Object {},
6161
"sourceNodes": Object {},
62+
"unstable_shouldOnCreateNode": Object {
63+
"version": "2.24.80",
64+
},
6265
},
6366
"ssr": Object {
6467
"onPreRenderHTML": Object {},

‎packages/gatsby/src/utils/api-node-docs.ts

+11
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,17 @@ export const sourceNodes = true
133133
*/
134134
export const onCreateNode = true
135135

136+
/**
137+
* Called before scheduling a `onCreateNode` callback for a plugin. If it returns falsy
138+
* then Gatsby will not schedule the `onCreateNode` callback for this node for this plugin.
139+
* Note: this API does not receive the regular `api` that other callbacks get as first arg.
140+
*
141+
* @gatsbyVersion 2.24.80
142+
* @example
143+
* exports.unstable_shouldOnCreateNode = ({node}, pluginOptions) => node.internal.type === 'Image'
144+
*/
145+
export const unstable_shouldOnCreateNode = true
146+
136147
/**
137148
* Called when a new page is created. This extension API is useful
138149
* for programmatically manipulating pages created by other plugins e.g.

‎packages/gatsby/src/utils/api-runner-node.js

+19
Original file line numberDiff line numberDiff line change
@@ -527,9 +527,28 @@ module.exports = async (api, args = {}, { pluginSource, activity } = {}) =>
527527
return null
528528
}
529529

530+
let gatsbyNode = pluginNodeCache.get(plugin.name)
531+
if (!gatsbyNode) {
532+
gatsbyNode = require(`${plugin.resolve}/gatsby-node`)
533+
pluginNodeCache.set(plugin.name, gatsbyNode)
534+
}
535+
530536
const pluginName =
531537
plugin.name === `default-site-plugin` ? `gatsby-node.js` : plugin.name
532538

539+
// TODO: rethink createNode API to handle this better
540+
if (
541+
api === `onCreateNode` &&
542+
gatsbyNode?.unstable_shouldOnCreateNode && // Don't bail if this api is not exported
543+
!gatsbyNode.unstable_shouldOnCreateNode(
544+
{ node: args.node },
545+
plugin.pluginOptions
546+
)
547+
) {
548+
// Do not try to schedule an async event for this node for this plugin
549+
return null
550+
}
551+
533552
return new Promise(resolve => {
534553
resolve(runAPI(plugin, api, { ...args, parentSpan: apiSpan }, activity))
535554
}).catch(err => {

0 commit comments

Comments
 (0)
Please sign in to comment.