diff --git a/data/onCreateNode/index.ts b/data/onCreateNode/index.ts index cadf7b576b..526dd3e121 100644 --- a/data/onCreateNode/index.ts +++ b/data/onCreateNode/index.ts @@ -1,5 +1,16 @@ import { GatsbyNode, Node } from 'gatsby'; +// Touch existing derived nodes on every build so Gatsby's stale-node GC +// does not delete them on warm restart. Derived nodes created via +// `createNode` from inside `onCreateNode` are not re-created when their +// parent File nodes are cached unchanged, so without this hook the +// ExampleFile nodes disappear between dev runs and `allExampleFile` +// returns undefined in createPages. +export const sourceNodes: GatsbyNode['sourceNodes'] = ({ actions, getNodesByType }) => { + const { touchNode } = actions; + getNodesByType('ExampleFile').forEach((node) => touchNode(node)); +}; + export const onCreateNode: GatsbyNode['onCreateNode'] = async ({ node, actions: { createNode, createParentChildLink }, diff --git a/gatsby-config.ts b/gatsby-config.ts index cc56590dd8..a6a5597171 100644 --- a/gatsby-config.ts +++ b/gatsby-config.ts @@ -67,7 +67,16 @@ const headerLinkIcon = `